-
-
Save sayems/3318863 to your computer and use it in GitHub Desktop.
Using Java Selenium Webdriver and LoadableComponent to handle page changes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class ExpectedPage extends SlowLoadableComponent<ExpectedPage> { | |
| private WebDriver driver; | |
| private String pagelabel; | |
| private static int timeOutInSeconds = 3; // this controls how long get() method will | |
| // delay after load() and before isLoaded() | |
| public ExpectedPage(WebDriver drv) { | |
| super(new SystemClock(), timeOutInSeconds); | |
| System.out.println("Loading ExpectedPage Page"); | |
| this.driver = drv; | |
| this.driver = new ReusableInternetExploderDriver(); | |
| this.get(); // requires being called explicitly. not sure why. | |
| PageFactory.initElements(driver, this); | |
| } | |
| @Override | |
| protected void isLoaded() throws Error { | |
| System.out.println("Calling isLoaded..."); | |
| // i'm not sure this block of code actually works but here it is | |
| // might need to come up with a better idea | |
| 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(); | |
| if ( pagelabel.equalsIgnoreCase("Expected Text") ) | |
| { | |
| System.out.println("Expected page is loaded."); | |
| assertTrue(true); | |
| } else { | |
| System.out.println("Expected page is not loaded. Actual page label is: " + pagelabel ); | |
| assertTrue(false); | |
| } | |
| } | |
| @Override | |
| protected void load() { | |
| System.out.println("Calling load..."); | |
| // do whatever is necessary here to load the page | |
| // and then isLoaded() will be called again | |
| } | |
| @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