getTitle()在Selenium Webdriver上给出了奇怪的结果

时间:2015-08-14 10:54:52

标签: javascript node.js selenium selenium-webdriver phantomjs

我在selenium-webdriver中运行自动化测试脚本并结合Phantomjs环境。

我试图通过node.js运行我的脚本(不使用Python或C#)

以下是我如何设置环境的步骤:

  1. 安装selenium webdriver:

    C:\xampp\htdocs\testPhantomJS\>npm install selenium-webdriver

  2. 安装phantomJS

    将phantomjs脚本放在以下位置: C:\xampp\htdocs\testPhantomJS\node_modules\selenium-webdriver

    enter image description here

  3. 运行独立的selenium服务器:

    enter image description here

  4. 这是我在下面运行的测试脚本[login-as-administrator.js:

    var webdriver = require('selenium-webdriver');
    var By = require('selenium-webdriver').By;
    var until = require('selenium-webdriver').until;
    var equals = require('selenium-webdriver').equals;
    var driver = new webdriver.Builder()
        .withCapabilities(webdriver.Capabilities.phantomjs())
        .build();
    var baseUrl = 'http://saswatr3.ouh.co/login';
    var expectedTitle = " Track Revenue ";
    
    driver.get(baseUrl);
    var actualTitle = driver.getTitle();
    console.log(actualTitle);
    if(expectedTitle === actualTitle)
    {
        console.log("Verification Successful - The correct title is displayed on the web page.");
    }
    else
    {
        console.log("Verification Failed - An incorrect title is displayed on the web page.");
    }
    driver.manage().window().maximize();
    driver.findElement(By.id('username')).sendKeys('saswat@matrixnmedia.com');
    driver.findElement(By.id('password')).sendKeys('DarkPrince2012');
    driver.findElement(By.id('_submit')).click();
    driver.wait(until.titleIs('Track Revenue'), 1000);
    driver.quit();
    

    我通过node.js

    运行上面的脚本

    C:\xampp\htdocs\testPhantomJS\node_modules\selenium-webdriver >node login-as-administrator.js

    当我运行此脚本时,我收到如下所述的以下报告:

    enter image description here

    正如您所看到的,将actualTitle放在日志上时,我得到了奇怪的结果。

    我无法弄明白,为什么这样奇怪的报道即将到来。有什么我错过了吗?

1 个答案:

答案 0 :(得分:3)

getTitle安排一个命令,该命令将在以后由WebDriverJS框架执行(在您的情况下,在加载页面时)。它返回一个承诺,而不是标题。

试试这个:

driver.getTitle().then(function(title) {

    if(expectedTitle === title){
        console.log("Verification Successful - The correct title is displayed on the web page.");
    }
    else{
        console.log("Verification Failed - An incorrect title is displayed on the web page.");
    }
});

而不是:

var actualTitle = driver.getTitle();
console.log(actualTitle);
if(expectedTitle === actualTitle)
{
    console.log("Verification Successful - The correct title is displayed on the web page.");
}
else
{
    console.log("Verification Failed - An incorrect title is displayed on the web page.");
}

此外,删除预期标题的额外空格,例如:

var expectedTitle = "Track Revenue";