我有以下问题。我使用Selenium 2.42.2测试我们公司的intranet站点。因此,我用一个测试套件实现了一个示例Testcase。我用Firefox 31和IE11来测试这个测试用例。所有的测试都很好,但是有时候IE似乎没有点击某些元素。这非常令人困惑,因为有时它正确地工作,有时它不起作用。然而,到目前为止,我尝试了以下解决方案,但没有成功:
唯一可行的解决方案是双击。但是,如果这个错误没有发生,那么firefox就会出现新的问题。
有没有人知道是什么原因导致了这个问题?
感谢你的帮助。
所附守则:
@RunWith(Parameterized.class)
public class SeleniumTest_WD_used extends AbstractSelenium {
public SeleniumTest_WD_used(RemoteWebDriver d) {
driver = d;
}
private String baseUrl = "company.intranet.site.com";
@Override
@Before
public void setUp() {
driver.get(baseUrl);
driver.manage().timeouts().implicitlyWait(500, TimeUnit.MILLISECONDS);
}
@Test
public void worldClock_autoCity_Test_with_ES() throws InterruptedException {
Thread.sleep(2000);
driver.findElement(By.xpath("some XPath")).click();
Thread.sleep(2000);
new Select(driver.findElement(By.id("some ID"))).selectByVisibleText("Some Text");
driver.findElement(By.cssSelector("Some Css_Element")).click();
driver.findElement(By.xpath("some XPath")).click();
RemoteWebElement e1 = (RemoteWebElement) driver.findElement(By.xpath("some XPath"));
Assert.assertEquals("Some Assert", e1.getText());
}
}
我以以下方式重写IE和FF驱动程序的“findElement”方法(但如果我使用标准方法,也会遇到这个错误):
public class FirefoxDriver2_0 extends FirefoxDriver {
private static FirefoxDriver2_0 instance = null;
private long startTime;
private long stopTime;
private FirefoxDriver2_0() {
super();
}
public static synchronized FirefoxDriver2_0 getInstance() {
if (instance == null) {
instance = new FirefoxDriver2_0();
}
return instance;
}
@Override
public RemoteWebElement findElement(By by) {
return elementSearch(by, FirefoxDriver2_0.getInstance());
}
private RemoteWebElement elementSearch(By by, FirefoxDriver driver) {
startTime = System.currentTimeMillis();
RemoteWebElement helpingElement = null;
isElementPresent(by);
try {
helpingElement = (RemoteWebElement) super.findElement(by);
} catch (Exception e) {
AllTest.updateLogger("[error] method 'elementSearch' incomplete");
fail("Test not successfull!");
} finally {
stopTime = System.currentTimeMillis();
timeWarning(by.toString());
}
return helpingElement;
}
private boolean isElementPresent(By by) {
try {
super.findElement(by);
return true;
} catch (NoSuchElementException e) {
stopTime = System.currentTimeMillis();
timeWarning(by.toString());
AllTest.updateLogger("[main] ERROR\tThe following expression could not be solved: " + by);
fail("Test not successfull! --> Error: Element not found. Please check the failed XPath's");
return false;
}
}
private void timeWarning(String s){
if(stopTime-startTime > 500){
AllTest.updateLogger("[main] WARNING\tHigh response-time detected: " + (stopTime-startTime) + " ms [@element: " + s + "]");
}
}
如果您需要进一步的代码或信息,请询问。我还有两个更相关的课程。)初始化测试和2.)作为我的测试用例的父类的抽象类。
发布于 2014-08-12 06:38:54
现在我找到解决办法了!
正如您在我的问题代码中所看到的,我重写了FF和IE驱动程序中的findElement方法。这是解决这个真正烦人的问题的关键。因为我发现IE有时很简单,不会单击某些元素(主要是发送ajax请求的元素)。我想这是因为IE没有直接关注这些元素。IE速度慢,需要更多的时间--您需要双击元素(1.设置焦点,2.真正单击)。现在有了解决办法:
您可以从IE中重写findElement方法,并添加一个简单的By-元素来保存请求的元素。如果现在这个方法找不到一个元素,因为没有真正单击父元素,那么您可以在这个方法中再次单击父元素,而vóila就可以工作了!
对于这个解决方案,构建一个自己的类(例如: InternetExplorerDriver2_0)非常重要,它是一个单例类(在自己的类中执行单击),它重写了标准的findElement方法。
(我想很明显,在下面的测试中,您必须使用已更改的IEDriver类而不是原始的'InternetExplorerDriver‘类)
在这里,您可以找到更改后的IEDriver类的代码:
import static org.junit.Assert.fail;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebElement;
public class InternetExplorerDriver2_0 extends InternetExplorerDriver {
private static InternetExplorerDriver2_0 instance = null;
private static DesiredCapabilities caps = null;
private long startTime;
private long stopTime;
private By olderBy;
private InternetExplorerDriver2_0() {
super(caps);
}
public static synchronized InternetExplorerDriver2_0 getInstance(
DesiredCapabilities capabilities) {
if (instance == null) {
caps = capabilities;
instance = new InternetExplorerDriver2_0();
}
return instance;
}
@Override
public RemoteWebElement findElement(By by) {
return elementSearch(by, InternetExplorerDriver2_0.getInstance(caps));
}
private RemoteWebElement elementSearch(By by, InternetExplorerDriver driver) {
startTime = System.currentTimeMillis();
RemoteWebElement helpingElement = null;
isElementPresent(by);
try {
helpingElement = (RemoteWebElement) super.findElement(by);
} catch (Exception e) {
System.out.println("[error] method 'elementSearch' incomplete");
fail("Test not successfull!");
} finally {
stopTime = System.currentTimeMillis();
timeWarning(by.toString());
}
olderBy = by;
return helpingElement;
}
private boolean isElementPresent(By by) {
try {
super.findElement(by);
return true;
} catch (NoSuchElementException e1) {
//the following lines are the important!!!
try {
InternetExplorerDriver2_0.getInstance(caps).findElement(olderBy).click();
super.findElement(by);
return true;
} catch (Exception e2) {
stopTime = System.currentTimeMillis();
timeWarning(by.toString());
AllTest.updateLogger("[main] ERROR\tThe following expression could not be solved: " + by);
fail("Test not successfull! --> Error: Element not found. Please check the failed XPath's");
return false;
}
}
}
private void timeWarning(String s) {
if (stopTime - startTime > 500) {
AllTest.updateLogger("[main] WARNING\tHigh response-time detected: " + (stopTime - startTime) + " ms [@element: " + s + "]");
}
}
}
我知道这不是一个完美清洁的解决方案,但这是一个很好的解决办法,节省了很多时间!
https://stackoverflow.com/questions/25261217
复制相似问题