2016-08-25 31 views
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: &#040;0&#041; 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

+0

的可能的复制[硒/ Firefox中:命令“点击()”不以发现单元工作(http://stackoverflow.com/questions/15294630/selenium-firefox-command-click -doesnt-work-with-a-found-element) –

+0

你确定提供的Id对于Chrome浏览器和Firefox浏览器都是一样的吗? –

+0

@SaurabhGaur你能澄清吗?我使用完全相同的代码在两个浏览器中运行此代码。在这一行上,我初始化驱动程序,我只是将其从Firefox更改为Chrome,并且运行其他所有内容完全相同。浏览器之间不应该有HTML ID发生变化的原因。 – Jrawr

回答

0

多的测试后,我设法找到答案。虽然titlebar_hyperlink_8-lbsignout在Chrome中充当选择器,但显然我必须在Firefox上更深层次地进行操作,并且它现在可以工作。

//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"); 
相关问题