I am working on a testing script using selenium webdriver, phantomJS, and Mocha.
My scripts are written in JavaScript.
These are the variables declared outside the test modules:
var afterLoginURL;
var afterLoginTitle;
var loginFlag = 0;
I have two test modules:
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.");
}
});
});
});
In the next test module, I navigate directly to the dashboard page and check for elements within the <ul>
and <li>
, but encounter an error locating the element:
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();
});
});
This is the error message being displayed:
NoSuchElementError: {"errorMessage":"Unable to find element with xpath
'//ul[contains(@id, 'sidemenu')]/li'"
What could be the issue here?
Is it because the session expired? How can I revisit the dashboard page (in another test module) without facing session expiry to detect the menu?
Here is the HTML structure:
<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>