我正在使用selenium webdriver,phantomJS,Mocha编写测试脚本。
我的脚本是javascript文件。
这是我的变量,在测试模块之外声明:
var afterLoginURL;
var afterLoginTitle;
var loginFlag = 0;
我有两个测试模块:
test.describe('Site Login Test', function()
{
test.it('User Login', function()
{
var driver = new webdriver.Builder()
.withCapabilities(webdriver.Capabilities.phantomjs())
.build();
driver.get(baseUrl);
driver.getTitle().then(function(title)
{
if(expectedTitle === title) /* if the title matches */
{
driver.findElement(By.id('username')).sendKeys(username);
driver.findElement(By.id('password')).sendKeys(password);
driver.findElement(By.id('_submit')).click();
if(username.length > 0 && password.length > 0)
{
driver.findElements(By.xpath("//a[contains(text(), 'Log out')]")).then(function(elem)
{
if(elem.length > 0)
loginFlag = 1;
else
console.log("Login Unsuccessful");
if(loginFlag == 1)
{
console.log("Login Successful");
// after login, we are in the dashboard page, counting a side menu <li> //
driver.findElements(By.xpath("//ul[contains(@id, 'sidemenu')]/li")).then(function(liSize)
{
console.log(liSize);
});
driver.getCurrentUrl().then(function(url){
afterLoginURL = url;
});
driver.getTitle().then(function(title){
afterLoginTitle = title;
});
}
else
{
console.log("Login Unsuccessful");
}
});
}
}
else /* if the title doesn't match */
{
console.log("Verification Failed - An incorrect title.");
}
});
});
});
现在,在下一个测试模块中,我将直接导航到仪表板页面,然后检查相同的<ul>
和<li>
,但似乎代码无法找到该元素:
test.describe('Menu Test', function()
{
test.it('Detect Menu', function()
{
var driver = new webdriver.Builder()
.withCapabilities(webdriver.Capabilities.phantomjs())
.build();
if(loginFlag == 1)
{
console.log("afterLoginURL :" +afterLoginURL);
console.log("afterLoginTitle :" +afterLoginTitle);
driver.get(afterLoginURL);
driver.getTitle().then(function(title)
{
if(afterLoginTitle === title)
{
console.log("Visited After Login Page");
driver.findElement(By.xpath("//ul[contains(@id, 'sidemenu')]/li")).then(function(elem){
console.log(elem.length);
// getting error in this section;
});
}
else
{
console.log("Something wrong has happened. Much bigger than calamity");
}
});
}
else
{
console.log("Not logged in");
}
driver.quit();
});
});
这是我收到的错误消息:
NoSuchElementError: {"errorMessage":"Unable to find element with xpath
'//ul[contains(@id, 'sidemenu')]/li'"
我在这里做错了什么?
是因为会话耗尽了吗?如何在没有会话到期的情况下重新访问仪表板页面(在其他测试模块中)来检测菜单?
这是HTML:
<ul class="nav nav-second-level collapse" id="sidemenu">
<li>
<a href="/admin/user/">All Users</a>
</li>
<li>
<a href="/admin/company/">All Companies</a>
</li>
<li>
<a href="/admin/device/">Devices</a>
</li>
<li>
<a href="/admin/email/">Send Email</a>
</li>
<li>
<a href="/admin/impersonate">Impersonate User</a>
</li>
<li>
<a href="/admin/encrypttest">Test Encryption</a>
</li>
</ul>
答案 0 :(得分:-1)
也许尝试一下CSS选择器,
driver.findElement(By.cssSelector("#sidemenu > li")).then(function(elem){
console.log(elem.length);
// getting error in this section;
});
此CSS选择器正在查找具有子LI
s的ID(#)sidemenu的元素。有几个给出了你提供的HTML。
BTW ...... elem.length
应该打印什么?您的意思是使用.findElementS()
代替并打印找到的元素数量吗?