[ Selenium Firefox driver passes test but doesn't actually click button ]
I am using Selenium and Cucumber to drive test in Firefox and Chrome. These tests work just fine in Chrome. However, in Firefox I'm experiencing an interesting problem. Every step in the scenario below says it passes:
Feature: Login Action
Scenario: Successful Login with Valid Credentials
Given User is on Home Page
When User enters "admin" and "password"
Then Message displayed Login Successfully
Scenario: Successful LogOut
When User LogOut from the Application
Then Message displayed LogOut Successfully
And I close the browser
However, when it gets to the "user logut from application" it never actually logs out. It says that the button was clicked but I am still logged.
@When("^User LogOut from the Application$")
public void user_LogOut_from_the_Application() throws Throwable {
driver.findElement(By.id("titlebar_hyperlink_8-lbsignout")).click();
System.out.println("The click worked");
}
@And("^I close the browser$")
public void i_close_the_browser() throws Throwable {
System.out.println("Driver= " + driver);
driver.close();
System.out.println("The browser should be closed");
}
It executes the final and, but the browser doesn't close and I'm still logged into the website.
The structure for that element looks like this (forgive the formatting I copied and pasted from the page source)
<table role="presentation" align="right" style="height: 100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<span id="titlebar-tb_username" class="homeButtontxtappname userfullname" style="display:none">MAXADMIN</span>
</td>
<td id="titlebar_bulletinboardlink_3-co_0" role='presentation' nowrap="nowrap" style="vertical-align:top;">
<span id="titlebar_bulletinboardlink_3-lbshowbboard" align='left' ctype="label" tabindex="0" targetid="titlebar_bulletinboardlink_3-lbshowbboard" mxevent="click" accesskey='B' class="text powerwhite anchor" style="display:block;cursor:pointer;" title="Bulletins: (0) ALT+B">
<img id='titlebar_bulletinboardlink_3-lbshowbboard_image' src='btn_bboard.gif' class='pwimg' border='0' style='vertical-align:top;margin:0px;margin-left:3px;margin-right:3px;' alt=''/>
<span>
<span/>
<span class='text hl hlak'>B</span>
<span>ulletins: (0) </span>
</span>
</span>
</td>
<td id="titlebar_hyperlink_5-co_0" role='presentation' nowrap="nowrap" align="left" style="vertical-align:top;">
<span id="titlebar_hyperlink_5-lbshowmenu_reportsmenu" align='left' ctype="label" tabindex="0" ev="reportsmenu" targetid="titlebar_hyperlink_5-lbshowmenu_reportsmenu" mxevent="click" accesskey='R' class="text powerwhite anchor" style="display:block;cursor:pointer;" title="Reports ALT+R">
<img id='titlebar_hyperlink_5-lbshowmenu_reportsmenu_image' src='btn_reporting.gif' class='pwimg' border='0' style='vertical-align:top;margin:0px;margin-left:3px;margin-right:3px;' alt=''/>
<span>
<span/>
<span class='text hl hlak'>R</span>
<span>eports</span>
</span>
</span>
</td>
<td id="titlebar_hyperlink_7-co_0" role='presentation' nowrap="nowrap" align="left" style="vertical-align:top;">
<span id="titlebar_hyperlink_7-lbshowmenu_profile" align='left' ctype="label" tabindex="0" ev="profile" targetid="titlebar_hyperlink_7-lbshowmenu_profile" mxevent="click" accesskey='P' class="text powerwhite anchor" style="display:block;cursor:pointer;" title="Profile ALT+P">
<img id='titlebar_hyperlink_7-lbshowmenu_profile_image' src='btn_profile.gif' class='pwimg' border='0' style='vertical-align:top;margin:0px;margin-left:3px;margin-right:3px;' alt=''/>
<span>
<span/>
<span class='text hl hlak'>P</span>
<span>rofile</span>
</span>
</span>
</td>
<td id="titlebar_hyperlink_8-co_0" role='presentation' nowrap="nowrap" align="left" style="vertical-align:top;">
<span id="titlebar_hyperlink_8-lbsignout" align='left' ctype="label" tabindex="0" targetid="titlebar_hyperlink_8-lbsignout" mxevent="click" accesskey='S' class="text powerwhite anchor" style="display:block;cursor:pointer;" title="Sign Out ALT+S">
<img id='titlebar_hyperlink_8-lbsignout_image' src='btn_signout.gif' class='pwimg' border='0' style='vertical-align:top;margin:0px;margin-left:3px;margin-right:3px;' alt=''/>
<span>
<span/>
<span class='text hl hlak'>S</span>
<span>ign Out</span>
</span>
</span>
</td>
<td id="titlebar_hyperlink_9-co_0" role='presentation' nowrap="nowrap" align="left" style="vertical-align:top;">
<span id="titlebar_hyperlink_9-lbshowmenu_help" align='left' ctype="label" tabindex="0" ev="help" targetid="titlebar_hyperlink_9-lbshowmenu_help" mxevent="click" accesskey='H' class="text powerwhite anchor" style="display:block;cursor:pointer;" title="Help ALT+H">
<img id='titlebar_hyperlink_9-lbshowmenu_help_image' src='btn_help.gif' class='pwimg' border='0' style='vertical-align:top;margin:0px;margin-left:3px;margin-right:3px;' alt=''/>
<span>
<span/>
<span class='text hl hlak'>H</span>
<span>elp</span>
</span>
</span>
</td>
</tr>
</table></div>
</div>
</p>
<p>I'm not getting any errors, It says I passed, but I'm still not getting signed out either. </p>
<p>EDIT: I attempted to utilize the following solution with no luck</p>
WebElement element = driver.findElement(By.id("titlebar_hyperlink_8-lbsignout_image"));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", element);
wait.until(
ExpectedConditions.visibilityOfElementLocated(By.
xpath("//img[@id='titlebar_hyperlink_8-lbsignout_image']")));
<p>This is the log from Chrome's console. </p>
<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-js lang-js prettyprint-override"><code> document.getElementById("titlebar_hyperlink_8-lbsignout_image")
<img id="titlebar_hyperlink_8-lbsignout_image" src="btn_signout.gif" class="pwimg" border="0" style="vertical-align:top;margin:0px;margin-left:3px;margin-right:3px;" alt>
document.getElementById("titlebar_hyperlink_8-lbsignout_image").click
click() { [native code] }
document.getElementById("titlebar_hyperlink_8-lbsignout").click();
undefined
document.getElementById("titlebar_hyperlink_8-lbsignout_image")
<img id="titlebar_hyperlink_8-lbsignout_image" src="btn_signout.gif" class="pwimg" border="0" style="vertical-align:top;margin:0px;margin-left:3px;margin-right:3px;" alt>
document.getElementById("titlebar_hyperlink_8-lbsignout_image").click();
undefined
Answer 1
After much testing, I managed to find an answer. While titlebar_hyperlink_8-lbsignout worked as a selector in Chrome, I apparently had to step in one level deeper on firefox and it works now.
//set the wait driver inside local scope just to be safe.
wait = new WebDriverWait(driver, 60);
Thread.sleep(3000);
wait.until(ExpectedConditions
.elementToBeClickable(By.id("titlebar_hyperlink_8-lbsignout_image")));
System.out.println(driver.findElement(By.id("titlebar_hyperlink_8-lbsignout_image")));
driver.findElement(By.id("titlebar_hyperlink_8-lbsignout_image")).click();
System.out.println("This is where I should have pressed the logout button");