2

My test fall down when I run test, I run the test again, test is pass, code do not change but the result is different. Why? This is my code:

    public ProductPage clickOnAccessories(){

        //Click on link “Accessories”
        waitForElementToBeDisplayed(driver.findElement(By.xpath("//a[text()='Product Category']")), 200);
        Actions act = new Actions(driver);
        act.moveToElement(driver.findElement(By.xpath("//a[text()='Product Category']")));
        act.perform();

        link_Accessories.click();

        //Expected: Page “Accessories” has been opened with correct url, headline, tab title
        Assert.assertTrue(driver.getCurrentUrl().equals("http://store.demoqa.com/products-page/product-category/accessories/"));
        Assert.assertTrue(driver.getTitle().contains("Accessories"));
        Assert.assertTrue(tab_title_Accessories.getText().contains("customers"));

        return PageFactory.initElements(getDriver(), ProductPage.class);
    }


public void waitForElementToBeDisplayed(final WebElement element, int defaultWaitTime) {
    for (int i = 500; i < defaultWaitTime; i += 100) {
        Utils.waitTime(500);
        if (isElementDisplayed(element)) { return; }
    }
}

protected boolean isElementDisplayed(final WebElement element) {
    boolean isDisplayed = false;
    try {
        final String tagName = element.getTagName();
        if (!tagName.isEmpty()) { isDisplayed = element.isDisplayed(); }
        return isDisplayed;
    } catch (final Exception e) { }
    return isDisplayed;
} 

This is my error:

org.openqa.selenium.ElementNotVisibleException: element not visible
  (Session info: chrome=55.0.2883.87)
  (Driver info: chromedriver=2.25.426923 (0390b88869384d6eb0d5d09729679f934aab9eed),platform=Windows NT 6.3.9600 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 51 milliseconds
Build info: version: 'unknown', revision: 'c7b525d', time: '2016-09-01 14:57:44 -0700'
System info: host: 'Gaga', ip: '192.168.1.6', os.name: 'Windows 8.1', os.arch: 'amd64', os.version: '6.3', java.version: '1.8.0_101'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.25.426923 (0390b88869384d6eb0d5d09729679f934aab9eed), userDataDir=C:\Users\Dragana\AppData\Local\Temp\scoped_dir10100_17794}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=55.0.2883.87, platform=WIN8_1, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: a24c7146c1accfdd3ef521786f4cb399
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
MY DZON
  • 109
  • 1
  • 4
  • 9

2 Answers2

0

You might need to use embedded methods to wait until target element becomes visible:

// Set your own value of timeoutInSeconds
WebDriverWait wait = new WebDriverWait(webDriver, timeoutInSeconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[text()='Product Category']")));
Andersson
  • 51,635
  • 17
  • 77
  • 129
  • My error now : org.openqa.selenium.TimeoutException: Expected condition failed: waiting for visibility of element located by By.linkText: iMacs (tried for 5 second(s) with 500 MILLISECONDS interval) – MY DZON Jan 07 '17 at 19:24
  • This is your next step after clicking `Product Category`, right? Try `wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[contains(text(), 'iMacs')]")));` – Andersson Jan 07 '17 at 19:43
  • I tried : wait.until(ExpectedConditions.visibilityOfElementLocated(By.‌​xpath("//a[contains(‌​text(), 'iMacs')]"))); wait.until(ExpectedConditions.elementToBeClickable(By.xpath(""))); – MY DZON Jan 07 '17 at 19:51
  • waitForElementToBeDisplayed(driver.findElement(By.linkText("‌​iMacs")), 200); My test again sometimes works, sometimes does not work even though I tried all this – MY DZON Jan 07 '17 at 20:12
0

You must try Webdriver wait to eliminate this error. Enter you xpath expression where you want to trigger wait.

WebDriverWait wait = new WebDriverWait(driver, 30);
    wait.until(ExpectedConditions.elementToBeClickable(By.xpath("<Enter your xpath expression here>")));
Maninder
  • 164
  • 6
  • My error now: org.openqa.selenium.TimeoutException: Expected condition failed: waiting for element to be clickable: By.xpath: //a[text()='Accessories'] (tried for 30 second(s) with 500 MILLISECONDS interval) – MY DZON Jan 07 '17 at 19:36
  • It will be helpful if you can provide HTML piece of code and screenshot of you front end where you want to click. In your HTML please check for iframe as well. – Maninder Jan 10 '17 at 05:01