如何在使用nodejs的Selenium Test上等待操作

时间:2019-07-15 17:27:59

标签: node.js selenium testing

我已经使用selenium webdriver和nodejs编写了该测试,我在提交表单和检查页面标题以确认是否成功之前增加了30秒,因为我使用浏览器兼容性工具,因此该测试正在远程计算机上运行,​​因此页面加载可能需要20分钟左右,是否有更好的方法可以通过driver.wait或sleep来完成。在填充字段之前不应该单击按钮,并且单击按钮后必须检查标题功能。

/*
    LambdaTest selenium automation sample example
    Configuration
    ----------
    username: Username can be found at automation dashboard
    accessKey:  AccessKey can be generated from automation dashboard or profile section

    Result
    -------
    Execute NodeJS Automation Tests on LambdaTest Distributed Selenium Grid
*/
const webdriver = require('selenium-webdriver');

/*
    Setup remote driver
    Params
    ----------
    platform : Supported platform - (Windows 10, Windows 8.1, Windows 8, Windows 7,  macOS High Sierra, macOS Sierra, OS X El Capitan, OS X Yosemite, OS X Mavericks)
    browserName : Supported platform - (chrome, firefox, Internet Explorer, MicrosoftEdge, Safari)
    version :  Supported list of version can be found at https://www.lambdatest.com/capabilities-generator/
*/




// gridUrl: gridUrl can be found at automation dashboard
const GRID_HOST = 'hub.lambdatest.com/wd/hub';

function fillandsubmitform() {
    // Setup Input capabilities
    const capabilities = {
        platform: 'macOS Mojave',
        browserName: "Safari",
        version: '12.0',
        resolution: '1280x960',
        network: true,
        visual: true,
        console: true,
        video: true,
        name: 'Form', // name of the test
        build: 'NodeJS build' // name of the build
    }

    // URL: https://{username}:{accessKey}@hub.lambdatest.com/wd/hub
    const gridUrl = 'https://' + USERNAME + ':' + KEY + '@' + GRID_HOST;

    // setup and build selenium driver object
    const driver = new webdriver.Builder()
        .usingServer(gridUrl)
        .withCapabilities(capabilities)
        .build();



    // navigate to a url, search for a text and get title of page
    driver.get('https://.....');


        driver.findElement(webdriver.By.css('#_contactform_topic > option:nth-child(3)')).click();
        driver.findElement(webdriver.By.id('lastname')).sendKeys('testSurname');
        driver.findElement(webdriver.By.id('_hemail')).sendKeys('test@gmail.com');
        driver.findElement(webdriver.By.id('_ePhone')).click();
        driver.findElement(webdriver.By.id('_Morning')).click();
        setTimeout(function(){
                 driver.findElement(webdriver.By.xpath("//button[contains(text(),'Send')]")).click();
                    }, 30000);
        setTimeout(function(){
                  check_title();
                    }, 30000);


    function check_title() {
        var promise = driver.getTitle().then(function(title) {
            if (title === 'Thank you for submission') {
                console.log('Success');
                driver.quit();
                return true;
            } else {
                console.log('Failed --' + title);
                driver.quit();
            }
        });
        return promise;
    }
}
fillandsubmitform();

0 个答案:

没有答案