The test passed, but the action did not happen. Selenium webdriver + testng

I am completely new in selenium webdriver, I use version 2.31, testng 6.8 and fire tests in IE 8. I write my tests according to this scheme: I have test classes where I have methods with testng @Test annotation. It looks like this:

@Test(description="click Save Button ", dependsOnMethods = { "edit form" })
public void clickSaveButton(ITestContext context) {
    page.clickSaveButton(driver);

}

Then, as you can see, I have a page class where the ids, xpaths, etc. are stored. It looks like this:

public void clickSaveButton(WebDriver driver){
    Configuration.clickfoundElement(By.id(conf.get("saveButton")), driver);
}

conf is an object representing a properties file. Finally, I have a Configuration class where I am doing something like this:

public static void clickfoundElement(By by, WebDriver driver){
    int attempts = 0;

    while(attempts < 10) {
        try {

            driver.findElement(by).click();
            break;
        } catch(NoSuchElementException e) {
            System.out.println("NoSuchElementException");
            Reporter.log("NoSuchElementException<br/>");
            if(attempts==9){
                throw(e);

            }
        }
        catch(StaleElementReferenceException e) {
            System.out.println("StaleElementReferenceException");
            Reporter.log("StaleElementReferenceException<br/>");
            if(attempts==9){
                throw(e);
            }
        }}

This prevents me from having a NoSuchElementException and a StaleElementReferenceException and works very well.

My first question is: is this approach right? The second and most important question is that from time to time I get the following problem:

Testng , "clickSaveButton" ( ) , clickSaveButton ( , ). "NoSuchElementException" (, -, html-). , NoSuchElementException , , ( , , ) , ( ) ?

.

0
4

. , - , - , (richFaces rich: modalPanel, ). , DOM "" , - . , , , ExpectedConditions.elementToBeClickable(By by), . . .

+1

, , . WebDriverWait, :

public void clickSaveButton(WebDriver driver){
    Configuration.clickfoundElement(By.id(conf.get("saveButton")), driver);
}

:

public void clickSaveButton(WebDriver driver) {
    WebDriverWait doWait = new WebDriverWait(driver, 15 , 100);
    WebElement elementToClick = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(conf.get("saveButton"))));
    elementToClick.click();
}

clickfoundElement(). StaleElementExceptions. A StaleElementException DOM , , , .

StaleElementExceptions, :

  • , , .
  • PageFactory .

PageFactories , :

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.PageFactory;

public class LoginExample {

  @FindBy(how = How.ID, using = "username")
  private WebElement usernameField;
  @FindBy(how = How.ID, using = "password")
  private WebElement passwordField;
  @FindBy(how = How.ID, using = "login")
  private WebElement submitButton;

  public LoginExample(WebDriver driver) {
    PageFactory.initElements(driver, this);
  }

  public void enterCredentialsAndSubmitForm(String username, String password) {
    usernameField.clear();
    usernameField.sendKeys(username);
    passwordField.clear();
    passwordField.sendKeys(password);
    submitButton.click();
  }
}

@FindBy - WebElement, , WebElement, , , , StaleElementException ( , ms Selenium, , ).

, factory , , , -.

Selenium Wiki.

+1

while. , while . , driver.findElements() driver.findElement() . , , .

0

, , , By, :

By saveButton = By.id("saveButton");

public By getSaveButton() {
  waitForElementPresent(saveButton, 15);
  return driver.findElement(saveButton);
}

public void waitForElementPresent(final By locator, int timeout) {
  ExpectedCondition e = new ExpectedCondition<Boolean>() {
    public Boolean apply(WebDriver driver) {
      return driver.findElements(locator).size() > 0;
    }
  };

  WebDriverWait wait = new WebDriverWait(driver, timeout);
  wait.until(e);

}

. save:

page.getSaveButton().click();

(), IE8, RemoteWebDriver. , -, , -, , () . , , , , . IE7,8 9 click() , javascript onclick  ((JavaScriptExecutor)driver).executeScript("onclickFunction();");

, , javascript onclick, , javascript .

As for the StaleElementReferenceException, I would instantiate a new page object in each test, this way you will not get obsolete elements, and you will start with a clean state. In JUnit4, this will be in your @Before method.

0
source

All Articles