如何在Selenium-Webdriver NodeJS测试中的函数之间等待?

时间:2019-07-13 10:21:05

标签: node.js selenium

我正在尝试进行测试以提交表单。我使用硒网格(通过LambdaTest工具)和NodeJS。我想测试是否可以在Safari浏览器上提交表单。当我看Lambdatest上的屏幕视频时,我看到一切都非常快。我想等待前10秒,直到页面完全加载并填充一半的字段等待然后一点点,另一半,上下滚动以查看所有表单,然后再提交,最后单击提交按钮。我尝试使用setTimeout方法,但是在页面加载功能check_pages运行之前,我一直在控制台上获取失败

    /*
        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/
    */

    // username: Username can be found at automation dashboard
    const USERNAME = '.......';

    // AccessKey:  AccessKey can be generated from automation dashboard or profile section
    const KEY = '....';

    // 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: 'Test 22', // 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://www....').then(function() {   

                setTimeout(function () {  

            driver.findElement(webdriver.By.id('_field_genderId_1')).click();
            driver.findElement(webdriver.By.id('_field_firstName')).sendKeys('NodeJS');       
            driver.findElement(webdriver.By.id('_field_lastname')).sendKeys('Test');
            driver.findElement(webdriver.By.id('_field_streetAndNumber')).sendKeys('on Lambda Selenium Grid');
            driver.findElement(webdriver.By.id('_field_zip')).sendKeys('69115');
            },7000);
            setTimeout(function () {  

            driver.findElement(webdriver.By.id('_field_city')).sendKeys('Berlin');
            driver.findElement(webdriver.By.id('_field_selectedLanguage')).sendKeys('Deutschland');
            driver.findElement(webdriver.By.id('_field_email')).sendKeys('test@gmael.com');
            driver.findElement(webdriver.By.id('_field_phone')).sendKeys('017625856884');
            driver.findElement(webdriver.By.id('_field_birthDate')).sendKeys('07.07.2019');
            driver.executeScript('window.scrollTo(document.body.scrollHeight/2,0);');
            driver.findElement(webdriver.By.id('chk_accept')).click();
            },10000);
            scrollUpDown();
            setTimeout(function () { 
            driver.findElement(webdriver.By.id('submitFormButton')).click(); 
            }, 15000);

            driver.wait(check_page, 20000);   


            function scrollUpDown(){
                setTimeout(function () {
                    driver.executeScript('window.scrollBy(0, -window.innerHeight / 2);');
                   },2000);
                   setTimeout(function () {
                      driver.executeScript('window.scrollBy(0, -window.innerHeight / 2);');
                     },2000);
                     setTimeout(function () {
                      driver.executeScript('window.scrollBy(0, -window.innerHeight / 2);');
                     },2000);
                     setTimeout(function () {
                      driver.executeScript('window.scrollBy(0, -window.innerHeight / 3);');
                     },2000);
                     setTimeout(function () {
                        driver.executeScript('window.scrollBy(0, -window.innerHeight / 3);');
                       },2000);
                       setTimeout(function () {
                        driver.executeScript('window.scrollBy(0, -window.innerHeight / 3);');
                       },2000);
                     setTimeout(function () {
                      driver.executeScript('window.scrollBy(0, window.innerHeight / 3);');
                     },2000);
                     setTimeout(function () {
                        driver.executeScript('window.scrollBy(0, window.innerHeight / 3);');
                       },2000);
                       setTimeout(function () {
                        driver.executeScript('window.scrollBy(0, window.innerHeight / 3);');
                       },2000);
                       setTimeout(function () {
                        driver.executeScript('window.scrollBy(0, window.innerHeight / 3);');
                       },2000);
                       setTimeout(function () {
                        driver.executeScript('window.scrollBy(0, window.innerHeight / 3);');
                       },2000);
                       setTimeout(function () {
                        driver.executeScript('window.scrollBy(0, window.innerHeight / 3);');
                       },2000);
            }
            function check_page(){
                var promise = driver.getTitle().then(function(title){
                    if(title==='Thank you for subbmission'){
                        console.log('Success');
                        return true;
                    }
                    else{
                        console.log('Failed --' + title);
                    }
                });
                return promise;
            }


        });
    }

fillandsubmitform();

控制台错误

c:\ Users ... \ newTest>节点index.js 失败的 失败的 失败的 失败

(节点:3524)UnhandledPromiseRejectionWarning:TimeoutError:等待时间在20222ms之后超时     在c:\ Users ....- webdriver \ lib \ webdriver.js:841:17     在process._tickCallback(内部/进程/next_tick.js:68:7) (节点:3524)UnhandledPromiseRejectionWarning:未处理的承诺被拒绝。引发此错误的原因可能是抛出了一个没有catch块的异步函数,或者是拒绝了一个.catch()无法处理的承诺。 (拒绝ID:1) (节点:3524)[DEP0018] DeprecationWarning:已弃用未处理的承诺拒绝。将来,未处理的承诺拒绝将以非零退出代码终止Node.js进程。

0 个答案:

没有答案