【问题标题】:getTitle() giving strange result on Selenium WebdrivergetTitle() 在 Selenium Webdriver 上给出奇怪的结果
【发布时间】:2015-08-14 10:54:51
【问题描述】:

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

我正在尝试通过 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:

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

这是我在下面运行的测试脚本 [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

当我运行这个脚本时,我得到如下报告:

如您所见,将actualTitle 记录在日志中时,我得到了奇怪的结果。

我想不通,为什么会有这么奇怪的报告。有什么我遗漏的吗?

【问题讨论】:

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


    【解决方案1】:

    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";
    

    【讨论】:

    • 天哪!你是天才。多谢兄弟。 @CViejo .. 我也会遇到一些问题,但我会接受这个。你能帮我解决其他问题吗?
    猜你喜欢
    • 2013-08-20
    • 2019-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多