Skip to content

Instantly share code, notes, and snippets.

@djangofan
Last active January 20, 2018 02:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save djangofan/2009098 to your computer and use it in GitHub Desktop.
Save djangofan/2009098 to your computer and use it in GitHub Desktop.
Using Java Selenium Webdriver and LoadableComponent to handle page changes
package qa.test;
/**
* Call this class with :
* ExpectedPage ep = new ExpectedPage().get();
* ep.method();
*
* /
public class ExpectedPage extends LoadableComponent<ExpectedPage> {
private WebDriver driver; // The instance of WebDriver is located in a Utils class
// A test class extends Utils and instantiates the driver.
// This local driver instance is a reference of that driver.
private String pagelabel;
public ExpectedPage( WebDriver drv ) {
System.out.println("Loading ExpectedPage Page");
this.driver = drv;
this.driver = new RemoteWebDriver( "localhost", 4444 );
PageFactory.initElements(driver, this);
}
@Override
protected void isLoaded() throws Error {
System.out.println("Calling isLoaded...");
final WebElement myDynamicElement = ( new WebDriverWait(driver, 10) )
.until(new ExpectedCondition<WebElement>() {
@Override
public WebElement apply(WebDriver d) {
return d.findElement(By.className("prettyboxlabel"));
}
});
pagelabel = myDynamicElement.getText();
assertTrue( "Expected page is not loaded.", pagelabel.equalsIgnoreCase("Expected Text") );
}
@Override
protected void load() {
System.out.println("Calling load...");
driver.get("http://mysite.com" );
}
@FindBy(name = "NAME_LAST") private WebElement namelast;
@FindBy(name = "NAME_FIRST") private WebElement namefirst;
@FindBy(name = "NAME_MIDDLE") private WebElement namemiddle;
@FindBy(name = "NAME_SUFFIX") private WebElement namesuffix;
public void setNameLast(String lname) {
clearAndType(defnamelast, lname);
}
public void setNameFirst(String fname) {
clearAndType(defnamefirst, fname);
}
public void setNameMiddle(String mname) {
clearAndType(defnamemiddle, mname);
}
public void setNameSuffix(String sname) {
clearAndType(defnamesuffix, sname);
}
public void clearAndType(WebElement field, String text) {
field.clear();
field.sendKeys(text);
}
public void waitSeconds(int secons) {
System.out.print("Pausing for " + secons + " seconds: ");
try {
Thread.currentThread();
int x = 1;
while(x <= secons) {
Thread.sleep(1000);
System.out.print(" " + x );
x = x + 1;
}
System.out.print('\n');
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
public boolean handleAlert() {
String alertMessage = "";
boolean handled = false;
try {
if( driver.switchTo().alert() != null ) {
Alert alert = driver.switchTo().alert();
alertMessage = alert.getText();
alert.dismiss();
handled = true;
if ( alertMessage.contains("LAST NAME") ) {
System.out.println("Caught name field error.");
}
}
} catch ( Exception e ) {
System.out.println("Failure to catch and dismiss alert message box.");
}
return handled;
}
@FindBy(id = "NEXTButton") private WebElement nextbutton;
public void clickNextButton() {
System.out.print("Clicking next button to Next page. ");
nextbutton.click();
while ( handleAlert() ) {
nextbutton.click();
}
waitSeconds(2); //TODO need to fix this hack to fix page synchronization
}
// the rest of the class is below , as necessary
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment