0
我使用Selenium和Cucumber来驱动Firefox和Chrome中的测试。这些测试在Chrome中运行得很好。但是,在Firefox中,我遇到了一个有趣的问题。下面在方案中的每一步说,它经过:硒火狐浏览器驱动程序通过测试,但实际上并没有点击按钮
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
然而,当它到达了“从应用程序的用户logut”它从来没有真正注销。它说,按钮被点击,但我仍然记录。
@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");
}
它执行最后和,但浏览器不关闭,我仍然登录到网站。
该元素的结构如下(原谅我的格式复制并从页面源代码粘贴)
<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>
我没有得到任何错误,它说我通过,但我还没有退出。
编辑:我尝试使用以下解决方案,没有运气
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']")));
这是Chrome的控制台日志。
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
的可能的复制[硒/ Firefox中:命令“点击()”不以发现单元工作(http://stackoverflow.com/questions/15294630/selenium-firefox-command-click -doesnt-work-with-a-found-element) –
你确定提供的Id对于Chrome浏览器和Firefox浏览器都是一样的吗? –
@SaurabhGaur你能澄清吗?我使用完全相同的代码在两个浏览器中运行此代码。在这一行上,我初始化驱动程序,我只是将其从Firefox更改为Chrome,并且运行其他所有内容完全相同。浏览器之间不应该有HTML ID发生变化的原因。 – Jrawr