Follow linked web page

34 views
Skip to the first unread message

David Patterson

unread,
15 Jul 2015, 15:03:1415/07/2015
to webd...@googlegroups.com
I've got a web page I've opened with Selenium 2 code. I'm able to fill in the needed fields and click the login button on it.

How can I detect when the replacement page is loaded and how can I process it? (Note, this is not a pop-up situation, but a page transition from page 1 to page 2.)

Thanks.

Dave

David Patterson

unread,
15 Jul 2015, 16:06:5015/07/2015
to webd...@googlegroups.com
I've gotten over this problem. But am now in the StaleElementReferenceException pit.

I load a page, fill in a couple of fields with the right values, find a button and click it. 

One question: When I programmatically click on the button, I wait using 

private boolean waitForLoad(RemoteWebDriver driver) {

JavascriptExecutor je = (driver);

return je.executeScript("return document.readyState").equals("complete");

} // end of method waitForLoad


with 
while (!waitForLoad(driver)) {}
.
I'm able to loop through some content, but the driver.getWindowHandle() returns the same value as on the first page. Not sure if that is as it should be or if I'm still processing the original page.  driver.getCurrentUrl() also returns the url of the first page. 

When I then try to loop through the anchor tags on the page, this works without throwing the stale element exception:

List<WebElement> anchors = driver.findElements(By.ByTagName.tagName( "a" ));


but when I try to loop through the anchors and do:
String href = anchor.getAttribute( "href" ); 
I get the stale element exception.

Does some of this look hopelessly naive?

Thanks.

Dave 


--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To unsubscribe from this group and stop receiving emails from it, send an email to webdriver+...@googlegroups.com.
To post to this group, send email to webd...@googlegroups.com.
Visit this group at http://groups.google.com/group/webdriver.
For more options, visit https://groups.google.com/d/optout.

darrell

unread,
16 Jul 2015, 09:02:4016/07/2015
to webd...@googlegroups.com
Calling javascript always makes me wonder if you are looking at the page like a programmer rather than as a tester. As someone testing a website I do not wait for the document.readyState to be equal to "complete". So my Selenium script don't do this either. I LITERALLY look at the website and determine the test steps. The test steps don't change just because I'm switching from manually testing to automated testing. For example,

  1. Go to website URL
  2. Enter username in the username input box
  3. Enter password in the password input box
  4. Click the Login button
  5. Wait until I see the Logout button
As a manual tester I know how to do this. Converting it to Selenium would be:

  1. driver.get(URL);
  2. driver.findElement(By.cssSelector(usernameLocator)).sendKeys(username);
  3. driver.findElement(By.cssSelector(passwordLocator)).sendKeys(password);
  4. driver.findElement(By.cssSelector(loginButtonLocator)).click()
  5. wdw.until(ExpectedConditions.visibilityOfElementLocatedBy(logoutButtonLocator));
This assumes that driver equals a WebDriver instance. The *Locator variables equal the CSS selector to find the various elements. The username and password variables are String with obvious values. And wdw is an instance of WebDriverWait. The trick here was breaking the test steps down to something which is easily converted to Selenium code. If I started with the coding then I haven't taken the time to define WHAT I want to do and jumped into HOW I want to do it.

Also with todays Web 2.0 applications, the page will change because of Ajax calls rather than just page loads. So if I am counting the number of links on a page and an Ajax call alters the page, it is possible the number of anchors has changed. I should start counting the anchors again. If I cannot count all the anchors fast enough then I have a problem that many testers just ignore. I keep counting as if the count hasn't changed even though it could have. So if I get a list of anchors using driver.findElements() and I take my time interacting with the anchors, the List of WebElement will go stale if an Ajax call alters the DOM. So you REALLY need to move the information you need out of the DOM and into your programming language variables. So if I have:

foreach WebElement in a List of WebElement {
get the href
do some stuff
}

I might want to change that to:

foreach WebElement in a List of WebElement {
copy the href to a List of String
}

Now that the href is copied to a String in the programming language, an update to the DOM will make the List of WebElement stale but I don't care because I have a List of String which isn't tied to the DOM.

Bottom line, don't overthink it. Realize what you REALLY need to do when you are manually testing it and how that can be translated to Selenium. I only start using JavascriptExecutor when I've profiled the code and need to improve performance (never prematurely optimize is a golden rule of performance improvement) or if I have some limitation of the website that forces me to roll my own solution. Just because I know how to do it in javascript doesn't mean that is the equivalent to how a user would verify the application. I have seen programmers use JavascriptExecutor because it is easier for them past an automation problem only to later realize it was a UX design error that rendered the application useless to the user. The test passed but the application was unusable.
Reply all
Reply to author
Forward
0 new messages