1

I am trying to automate a test on my site using selenium web driver for javascript.

How can I approach running tests here using the wait method with content that may not be ready when the page loads eg data comes from an external api etc?

In my example my content is getting loaded by an external js file. You can see what the page looks like in this fiddle I couldn't link it in my code below as the fiddle gets wrapped in an iframe.

    <head>
    <script src="https://cdn.auth0.com/js/lock/10.2/lock.min.js"></script>
    </head>

    <body onload="lock.show();">
            <div id="content">
            <script type="text/javascript">
    var domain = 'contoso.auth0.com';
    var clientID = 'DyG9nCwIEofSy66QM3oo5xU6NFs3TmvT';

    var lock = new Auth0Lock(clientID, domain);
    lock.show({
    focusInput: false,
    popup: true,
    }, function (err, profile, token) {
    alert(err);
    });
            </script>
    </div>
    </body>

I can get it working using sleep, but can't guarentee my content will be ready after the timeout has finished.

    const {Builder, By, Key, until} = require('selenium-webdriver');

    let driver = new Builder()
            .forBrowser('firefox')
            .build();

    driver.get('MY_URL')
    driver.sleep(2000).then(function() {
        driver.findElement(By.name('email')).sendKeys('test@test.com')
        driver.findElement(By.name('password')).sendKeys('test')
        //driver.findElement(By.className('auth0-lock-submit')).click()
    })

But if I try with wait

    function login() {
        return driver.findElement(By.name('email')).sendKeys('test@test.com')
    }

    driver.get('MY_URL')
    driver.wait(login, 5000)

I get NoSuchElementError: Unable to locate element: *[name="email"]

How can I get this working so that I wait for my content to be available before proceeding.

ak85
  • 3,626
  • 16
  • 56
  • 107
  • Is this nightwatch or ??? You should add a tag for the language that you are looking for. Do you not have the equivalent of `WebDriverWait`? My background is C#/Java and I would just wait for the email element, etc. to be displayed. – JeffC Aug 31 '17 at 04:01
  • Possible duplicate of [Nightwatch: Better way than \`.pause(1000)\` to avoid brittle tests?](https://stackoverflow.com/questions/33224546/nightwatch-better-way-than-pause1000-to-avoid-brittle-tests) – JeffC Aug 31 '17 at 04:03
  • I work with Selenium-Java and Selenium-Python, I am not aware about the syntax in Selenium-JavaScript. But I can suggest you a strategy. Will it be acceptable for you? – DebanjanB Aug 31 '17 at 07:57

1 Answers1

1

The implicit wait will tell to the web driver to wait for certain amount of time before it throws a "No Such Element Exception". The default setting is 0. Once we set the time, web driver will wait for that time before throwing an exception..

driver.manage().timeouts().implicitlyWait(TimeOut, TimeUnit.SECONDS);   

Try to use FluentWait. Create a by function of your element which you want to wait and pass it in below method

WebElement waitsss(WebDriver driver, By elementIdentifier){
Wait<WebDriver> wait =
new FluentWait<WebDriver>(driver).withTimeout(60, TimeUnit.SECONDS) .pollingEvery(1, TimeUnit.SECONDS).ignoring(NoSuchElementException.class);

return wait.until(new Function<WebDriver, WebElement>()
{
public WebElement apply(WebDriver driver) {
return driver.findElement(elementIdentifier);
}});
}

Code for Explicit wait:

WebDriverWait wait = new WebDriverWait(driver, 60);
WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//span[contains(.,'Next')]")));

refer :-

https://www.guru99.com/implicit-explicit-waits-selenium.html

Shubham Jain
  • 13,158
  • 9
  • 60
  • 102