【发布时间】:2016-01-31 08:08:11
【问题描述】:
我目前正在为 Web 应用程序开发自动化测试套件。 Web 应用程序真的很挑剔。最近,我们有一个 UI 更新,所以我运行测试来测试我们的 GUI 和按钮,以及访问我们的 xpath 是否仍在工作。
万圣节前夜,我的剧本变成了现实SPOOOOOOOKY~
在这些套件中,我有一组两个测试:yesLogout、noLogout。 yesLogout 在您尝试注销时单击“是”按钮。 noLogout 在您尝试注销时单击“否”按钮。
在测试脚本中运行 noLogout 时,脚本点击“否”,注销提示消失。
当您在测试脚本中运行 yesLogout 时,脚本会单击“是”按钮,但没有任何反应。 “是”按钮使动画看起来好像被点击了一样,但提示仍然存在。
手动点击按钮就可以了。
我将分享这两种方法(noLogout、yesLogout),因为它们实际上是相同的,它们只是调用了两个不同的 xpath。为了这个问题,忘记我正在测试 - 现在测试不是问题。假装我是一个真正需要我的脚本才能点击此按钮的怪人。
测试中使用的两个函数(极其简化):
public class HiStackOverflow {
public class yesLogout {
public static void run(WebDriver driver) throws FileNotFoundException {
Properties prop = new Properties();
InputStream selectproductsidebar;
selectproductsidebar = new FileInputStream(
"C:/PATH/TO/PROPERTIES");
try {
prop.load(selectproductsidebar);
} catch (IOException e) {
e.printStackTrace();
}
WebElement yesLogout = driver.findElement(By.xpath(prop.getProperty("yeslogout")));
Actions actions = new Actions(driver);
actions.moveToElement(yesLogout).build().perform();
yesLogout.click();
}
}
public class noLogout {
public static void run(WebDriver driver) throws FileNotFoundException {
Properties prop = new Properties();
InputStream selectproductsidebar;
selectproductsidebar = new FileInputStream(
"C:/PATH/TO/PROPERTIES");
try {
prop.load(selectproductsidebar);
} catch (IOException e) {
e.printStackTrace();
}
WebElement noLogout = driver.findElement(By.xpath(prop.getProperty("nologout")));
Actions actions = new Actions(driver);
actions.moveToElement(noLogout).build().perform();
noLogout.click();
}
}
}
属性文件中Yes按钮和No按钮的xpath:
yeslogout = //div[contains(@title,'Enter')]
nologout = //div[contains(@title,'Esc')]
这里是与问题相关的 HTML:
<div style="transform: translate(0px, 60px) scale(1, 1) rotate(0deg);">
<div style="transform: translate(10px, 0px) scale(1, 1) rotate(0deg);">
<div>
<div tabindex="6" role="button" aria-label="Yes" title="Keyboard shortcut: Enter">
<div>
</div>
<div tabindex="7" role="button" aria-label="No" title="Keyboard shortcut: Esc" style="transform: translate(163.5px, 0px) scale(1, 1) rotate(0deg);">
<div>
</div>
</div>
</div>
</div>
tabindex="6" 是“是”按钮。 tabindex="7" 是否按钮。两者有一点点不同(no 按钮有某种转换:附加魔法转换),但就我而言,它不应该表现得那么奇怪。
再一次,实际问题 - 运行 yesLogout 方法定位、悬停并单击“是”按钮,但没有任何反应。手动单击“是”按钮效果很好。
如果需要,我可以在 html 中扩展更多 <div>s,但我真的不认为它会显示太多(只是更多的格式和 <canvas> 的东西)。我真的很想解释正在发生的事情。提前致谢。
编辑: 我曾尝试使用 JavascriptExecutor 来单击该元素,但很可惜,它不起作用。这整个周末都在绞尽脑汁。
甚至做一些疯狂的事情,比如:
WebElement yesLogout = driver.findElement(By.xpath(prop.getProperty("yeslogout")));
Actions actions = new Actions(driver);
actions.moveToElement(yesLogout, 10, 10).build().perform();
yesLogout.click();
yesLogout.click();
yesLogout.click();
yesLogout.click();
yesLogout.click();
yesLogout.click();
显示被反复点击的按钮,但没有实际功能。
为了完整起见并试图让人们理解这个问题,我会重申一下,手动点击按钮效果很好。与 Enter 快捷方式一样。 Selenium .click() 命令可见“工作”,但不执行按钮本身的功能。
第二次编辑: 我将包含一个完全扩展的相关 div 的 HTML:
<div style="transform: translate(794px, 202px) scale(1, 1) rotate(0deg);" tabindex="5" role="dialog">
<div>
<div>
<div>
<div>
<div>
<canvas height="106" width="332" style="top: 0px; left: 0px; width: 332px; height: 106px;" />
</div>
</div>
<div style="transform: translate(10px, 10px) scale(1, 1) rotate(0deg);">
<div>
<div style="transform: translate(6px, 6px) scale(1, 1) rotate(0deg);">
<div style="font-family: franklin-gothic-urw; font-weight: 400; font-style: normal; font-size: 14px; opacity: 1; color: rgb(0, 0, 0);">Are you sure you want to logoff?</div>
</div>
</div>
</div>
<div style="transform: translate(0px, 60px) scale(1, 1) rotate(0deg);">
<div style="transform: translate(10px, 0px) scale(1, 1) rotate(0deg);">
<div>
<div tabindex="6" role="button" aria-label="Yes" title="Keyboard shortcut: Enter">
<div>
<div>
<div>
<div>
<div>
<canvas height="36" width="148" style="top: 0px; left: 0px; width: 148.5px; height: 36px;" />
</div>
</div>
<div style="transform: translate(56px, 7px) scale(1, 1) rotate(0deg);">
<div style="font-family: proxima-nova; font-weight: 400; font-style: normal; font-size: 17px; opacity: 1; color: rgb(255, 255, 255); transform: translate(5px, 0px) scale(1, 1) rotate(0deg);">Yes</div>
</div>
<div style="">
<div>
<div>
<canvas height="36" width="148" style="top: 0px; left: 0px; width: 148.5px; height: 36px;" />
</div>
</div>
<div style="display: none;">
<div>
<canvas height="36" width="148" style="top: 0px; left: 0px; width: 148.5px; height: 36px;" />
</div>
<div style="transform: translate(56px, 7px) scale(1, 1) rotate(0deg);">
<div style="font-family: proxima-nova; font-weight: 400; font-style: normal; font-size: 17px; opacity: 1; color: rgb(255, 255, 255); transform: translate(5px, 0px) scale(1, 1) rotate(0deg);">Yes</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div tabindex="7" role="button" aria-label="No" title="Keyboard shortcut: Esc" style="transform: translate(163.5px, 0px) scale(1, 1) rotate(0deg);">
<div>
<div>
<div>
<div>
<div>
<canvas height="36" width="148" style="top: 0px; left: 0px; width: 148.5px; height: 36px;" />
</div>
</div>
<div style="transform: translate(58px, 7px) scale(1, 1) rotate(0deg);">
<div style="font-family: proxima-nova; font-weight: 400; font-style: normal; font-size: 17px; opacity: 1; color: rgb(255, 255, 255); transform: translate(5px, 0px) scale(1, 1) rotate(0deg);">No</div>
</div>
<div style="display: none;">
<div>
<div>
<canvas height="36" width="148" style="top: 0px; left: 0px; width: 148.5px; height: 36px;" />
</div>
</div>
<div style="display: none;">
<div>
<canvas height="36" width="148" style="top: 0px; left: 0px; width: 148.5px; height: 36px;" />
</div>
<div style="transform: translate(58px, 7px) scale(1, 1) rotate(0deg);">
<div style="font-family: proxima-nova; font-weight: 400; font-style: normal; font-size: 17px; opacity: 1; color: rgb(255, 255, 255); transform: translate(5px, 0px) scale(1, 1) rotate(0deg);">No</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
【问题讨论】:
-
您能否包含更多导致此问题的 html。我注意到 no 按钮包含在 yes 按钮中,这有点奇怪,并且该错误可能是由内部其他东西引起的。另外,如何将点击事件绑定到是按钮?
-
另外,
return false;还有其他绑定事件吗? -
点击事件是用一个
WebElement yesLogout = driver.findElement(By.xpath(prop.getProperty("yeslogout")))从属性文件加载xpath,而不是我使用一个简单的yesLogout.click()来点击它。我不相信有任何返回 false 的值。 -
我指的是实际页面上的 JavaScript。
-
我在这里做个猜测。按钮的单击处理程序是否可能由您的 JS 动态设置?当您手动运行它时,它有足够的时间进行设置并且点击发生得很好,但是 selenium 做得太快了吗?您可以尝试在单击之前添加 5 秒等待,看看是否有效吗?您还可以在单击并打印单击处理程序之前在页面中运行一些 javascript,以查看它是否设置正确(可能有助于调试)。看到这个:stackoverflow.com/questions/5327668/…
标签: javascript java html selenium xpath