Mehar44 Mehar44 - 11 months ago 78
HTML Question

How to write a test for clicking an option in the drop down menu with protractor?

I am writing a simple protractor test for my website login and logout functionality.
the login test is running fine, but I don't know how to write a test for logout as the logout link appears in the drop down menu after hovering on an icon

<div class="dropdownx">
<a href="#link" class="my-account"><i class="fa fa-user"></i></a>
<div class="dropdownx-content" style="text-align:center; margin-left:-60px; ">
<p><a href="#account">My Account</a></p>
<p><a href="#order_history">Order History</a></p>
<p><a href="#downloads">Downloads</a></p>
<p><a href="#transactions">Transactions</a></p>
<p><a href="#wishlist" id="wishlist-total">Wish List (0)</a></p>
<p><a href="#logout">Logout</a></p>

How can I access the last
for logout option as it has no
and also the element would be hidden as it appears only on
I have written this test for login.

describe('To test mdpocket login window', function(){
it('should be able to login', function() {
var login = browser.driver.findElement(by.css('#button-login'));

So basically my question is How can I get the hidden element with no id , no class. I have searched but could not find any solution so far. Please help !

I have tried this for logout but it is giving this error
Process exited with error code 1

var dropDown = browser.driver.findElement(by.css('.dropdownx'));
browser.driver.findElement(By.xpath("//a[text() = 'Logout']")).click();

My Solution :

var dropDown = browser.driver.findElement(by.css('.my-account'))
browser.driver.findElement(By.xpath("//a[text() = 'Logout']")).click();

This thing worked for me!

Answer Source

First, you need to move the mouse cursor and keep on drop down element. So all drop down menu options becomes visible. Now move cursor to logout link and click. You can follow the below code:

Code Snippet

 var EC=protractor.ExpectedConditions;
 var dropDown=element(by.css(".dropdownx"));
 var logoutLink=element(by.css('a[href="#logout"]'));

   browser.wait(EC.visibilityOf(dropDown),8000,'Logout Link is not visible');

FYI: Update drop down and logout link locators in case above mentioned locators failed to findout the elements