我正在尝试使用mocha在selenium环境上运行一个自动测试脚本。
脚本首先登录,然后检查all <p>
标记的文本。
如果任何<p>
标签的文本匹配短语""欢迎进入管理页面""那么登录尝试将被认为是成功的。否则就是失败。
我想更改代码。我不需要遍历所有
标记,只需要检查第一个
标记。我该怎么做呢?
我想记录细节,无论尝试是成功还是失败。
我的代码如下:
var assert = require('assert');
var test = require('selenium-webdriver/testing');
var webdriver = require('selenium-webdriver');
var By = webdriver.By;
var until = webdriver.until;
var equals = webdriver.equals;
test.describe('TrackRevenue Test', function()
{
test.it('should work', function()
{
var driver = new webdriver.Builder()
.withCapabilities(webdriver.Capabilities.phantomjs())
.build();
var loginFlag = 0;
var baseUrl = 'http://saswatr3.ouh.co/login';
var expectedTitle = "Track Revenue";
var successMessage = "Welcome to the admin page!";
driver.get(baseUrl);
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.");
}
});
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.findElements(By.tagName('p')).then(function (pTag)
{
pTag.forEach(function(p) //I am iterating through all the <p>. I need to get only the first <p>
{
p.getText().then(function(text)
{
if(text.toLowerCase() === successMessage.toLowerCase())
{
loginFlag = 1;
}
//done(); //mocha async callback
});
});
if(loginFlag ==1)
console.log("Login Successful");
else
console.log("Login Unsuccessful");
});
driver.quit();
});
});
<p>
部分的html部分有"欢迎来到管理页面"
<div class="wrapper wrapper-content animated fadeIn">
<div class="row">
<div class="col-centered col-xs-12">
<div class="ibox float-e-margins">
<div class="ibox-title">
<h5>Links</h5>
<div class="ibox-tools"> </div>
</div>
<div class="ibox-content">
<p>Welcome to the admin page!</p>
<p>Please use the menu on the left</p>
</div>
</div>
</div>
</div>
</div>
PS。我需要对代码进行更多的修改。在任何情况下,如果登录失败,那么登录页面有一个div,显示文本:&;无效凭据。&;
部分的html是这样的:
<div class="text-danger text-danger-orange">Invalid credentials.</div>
我想有一个检查脚本检测这部分以及
编辑:当我把这段脚本,然后我的脚本运行良好:
driver.findElements(By.xpath("//p[contains(text(), 'Welcome to the admin page!')]")).then(function()
{
loginFlag = 1;
if(loginFlag ==1)
console.log("Login Successful");
else
console.log("Login Unsuccessful");
});
但是当我放入这段脚本时,我得到一个类型错误:Undefined不是函数
if(driver.findElements(By.xpath("//p[contains(text(), 'Welcome to the admin page!')]")).size() != 0)
{
loginFlag = 1;
}
else
{
//"Welcome to the admin page" not found
if(driver.findElements(By.xpath("//div[contains(text(), 'Invalid Credentials!')]")).size() != 0)
{
//do what you have to do if "Invalid credentials" is found
}
else
{
// "Invalid credentials not found"
}
}
使用XPATH可以使您的工作更轻松。例如:
if(driver.findElements(By.xpath("//* [text()[contains(.,'Welcome to the admin page!')]]")).size() != 0){
//do what you have to do if "Welcome to the admin page" is found
}else{
//"Welcome to the admin page" not found
if(driver.findElements(By.xpath("//* [text()[contains(.,'Invalid credentials')]]")).size() != 0){
//do what you have to do if "Invalid credentials" is found
}else{
// "Invalid credentials not found"
}
}
我希望这对你有帮助。
编辑:如你所愿稍作阐述:
使用xpath,您可以简单地搜索页面上应该出现的所需文本。如果你只是想寻找字符串"欢迎来到管理页面"或"无效凭据"不管在页面上的位置,你可以很容易地搜索文本:
//* [text()[contains(.,'<your string text here>')]]
检查节点的getText()
方法是否包含给定的文本。
如果你想检查整个页面,如果有一个结果,你可以使用findElements
,因为它会返回所有找到的webelement。如果在给定的xpath中没有找到任何内容,它将返回一个大小为0的空"数组"。所以如果你使用
driver.findElements(By.xpath("//* [text()[contains(.,'Welcome to the admin page!')]]")).size() != 0
可以检查方法是否找到了包含所需文本的元素。
这就是全部,其他的东西只是简单的if/else..您可以根据需要使用上面提到的xpath和函数。因此,您可以简单地使用上面提到的xpath,而不是遍历所有<p>
标签。
EDIT2:
摩卡你可以试着这样做:
driver.findElements(By.xpath("//p[contains(text(), 'Welcome to the admin page!')]")).then(function(elements_arr)
{
if(elements_arr.length > 0){
//your element got found
console.log("Login Successful");
}else{
//your element was not found
console.log("Login Unsuccessful");
driver.findElements(By.xpath("//div[contains(text(), 'Invalid Credentials!')]")).then(function(elements_arr2)
{
if(elements_arr2.length > 0){
//your element invalid credentials got found
console.log("Login Unsuccessful, div invalid credentials found");
}else{
//your element was not found
console.log("Login Unsuccessful, div invalid credentials not found");
}
});
}
});