Java Selenium脚本在Firefox浏览器上失败
我正在使用- 使用java的Selenium 3.0.1 Firefox版本:50.1.0 geckodriver-v0.13.0-win64 我已经为我的web应用程序创建了一套完整的测试套件。当我在Java Selenium脚本在Firefox浏览器上失败,java,selenium,selenium-webdriver,geckodriver,Java,Selenium,Selenium Webdriver,Geckodriver,我正在使用- 使用java的Selenium 3.0.1 Firefox版本:50.1.0 geckodriver-v0.13.0-win64 我已经为我的web应用程序创建了一套完整的测试套件。当我在chrome浏览器上运行它时,所有的测试都运行得很顺利。除了浏览器兼容性测试,没有任何问题,我切换到firefoxdriver并运行测试,我的测试总是失败。输入字段出现问题,我猜输入文本的速度太快了,因此,即使文本没有填充到文本框中,但它正在单击提交按钮,因为我能够看到空白字段验证消息。我不知道如
chrome
浏览器上运行它时,所有的测试都运行得很顺利。除了浏览器兼容性测试,没有任何问题,我切换到firefox
driver并运行测试,我的测试总是失败。输入字段出现问题,我猜输入文本的速度太快了,因此,即使文本没有填充到文本框中,但它正在单击提交按钮,因为我能够看到空白字段验证消息。我不知道如何处理这个问题。我是否需要在FF上调试和运行我的整个脚本,或者是否有任何可行的解决方案
Eidted
我有一个DriverSetup
类,其中放置了以下代码-
public class DriverSetup
{
public static WebDriver driver;
@BeforeTest
@Parameters("browser")
public void setUp( @Optional String browser) throws IOException
{
try
{
switch(browser)
{
case "chrome":
System.out.println("Starting chrome........");
System.setProperty("webdriver.chrome.driver","D:/Application/ChromeDriver/chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get(TestData.mainSiteURL);
driver.manage().timeouts().implicitlyWait(45, TimeUnit.SECONDS);
ScreenCapture.passedScreenShot();
break;
case "firefox":
System.out.println("Starting Firefox........");
System.setProperty("webdriver.gecko.driver","D:/Application/geckodriver.exe");
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get(TestData.mainSiteURL);
driver.manage().timeouts().implicitlyWait(45, TimeUnit.SECONDS);
ScreenCapture.passedScreenShot();
break;
}
}
catch(Exception e)
{
System.out.println("Exception In : "+Thread.currentThread().getStackTrace()[1].getClassName()+"-->"+Thread.currentThread().getStackTrace()[1].getMethodName());
ScreenCapture.failedScreenShot();
}
}
}
另一个是SubmitNewContact
class
public class SubmitNewContact extends DriverSetup
{
@Test
public void submitNewContact() throws IOException
{
try
{
driver.findElement(By.linkText("Contact")).click();
driver.findElement(By.id("Name")).sendKeys(TestData.contactName);
driver.findElement(By.id("Phone")).sendKeys(TestData.contactPhone);
driver.findElement(By.id("EmailID")).sendKeys(TestData.contactEmail);
driver.findElement(By.id("message")).sendKeys(TestData.contactMessage);
ScreenCapture.passedScreenShot();
driver.findElement(By.xpath("//button[@type='submit']")).click();
ScreenCapture.passedScreenShot();
Thread.sleep(3000);
}
catch(Exception e)
{
System.out.println("Exception In : "+Thread.currentThread().getStackTrace()[1].getClassName()+"-->"+Thread.currentThread().getStackTrace()[1].getMethodName());
ScreenCapture.failedScreenShot();
}
}
}
并使用testNG运行这两个类-
<test name="ExecuteTestFirefox">
<parameter name="browser" value="firefox" />
<classes>
<class name="com.testmaster.DriverSetup"/>
<class name="com.test.user.SubmitNewContact"/>
</classes>
</test>
注意:-还为我的应用程序的每个功能提供了一些其他类,如
SubmitNewContact
,然后使用testing.xml
运行,相同的代码在chrome上工作?根据我的经验,geckodriver仍然存在这样的问题,这使得它很难使用。如果您无法为此创建解决方案,并且切换到chromedriver(或其他驱动程序)不是一个选项,我建议您使用“旧”Firefoxdriver,而不是geckodriver/marionette
但当然,在这里发布您的问题代码,这样我们就可以先解决一些问题。:) 当您遇到问题时,一个好的帮助/解决方案总是需要额外的等待时间。为了做到这一点,我建议您使用如下函数,等待找到元素:
public static void implicitlyWaitForElementPresent(FirefoxDriver driver, By elementToBePresent) {
// Waits up to 45 seconds while searching for the given Element to be present
driver.manage().timeouts().implicitlyWait(45, TimeUnit.SECONDS);
try {
driver.findElement(elementToBePresent);
} catch (org.openqa.selenium.NoSuchElementException e) {
e.printStackTrace();
}
}
您可以按以下方式使用此方法:
public void clickSomething() {
Utility.implicitlyWaitForElementPresent(mDriver, By.xpath("//*[@id='wrapper']/div[2]/div/ul[3]/li/a"));
WebElement something = mDriver.findElement(By.xpath("//*[@id='wrapper']/div[2]/div/ul[3]/li/a"));
something.click();
}
请注意,出于面向对象的原因,我将wait函数放在了另一个类中。
您可以根据需要更改此代码
如果您还有任何问题,我会来帮助您。我遇到了完全相同的问题。在某些表单上,
sendKeys()
方法正在发送密钥,我可以在屏幕截图中看到它们,但是我得到的消息是我必须填写表单。在其他页面上,它运行良好。同样在chrome中,我使用完全相同的代码,我没有任何问题
是否有人为selenium 3.0.1提供了解决方案
信息:我正在使用firefox docker设置
public void sendKeys(By objectPath, CharSequence key){
RemoteWebDriver browserDriver = DriverFactory.getInstance().getDriver();
WebDriverWait wait = new WebDriverWait(browserDriver, BuildDriverHandler.timeout);
wait.until(ExpectedConditions.visibilityOfElementLocated(objectPath));
wait.until(ExpectedConditions.elementToBeClickable(objectPath)).sendKeys(key);
}
你能分享你的代码中测试首先失败的部分吗,我可以帮你吗?我读了“调试脚本”-如果是JavaScript相关的问题,请更新标签:)@minhundje,添加了代码,请看看错误消息是什么?@mosaad,没有错误。但是无法在表单中填充数据,失败在哪里?没有在文本字段中输入文本,似乎操作太快,这就是它跳过的原因。怎么会太快?可能在单击之后,您需要等待此字段渲染,您是否尝试过与一些服务员一起使用?我使用
chrome
创建了我的脚本,因此无论我在哪里遇到问题,我都会管理等待。现在我想在firefox
上运行同一个套件,因此它似乎要在FF上调试整个套件并根据需要进行修改。假设我这样做了,那么如何确保它在其他浏览器或之前的浏览器上正常工作?有没有通用的解决方案可以摆脱这种情况?不幸的是,没有通用的解决方案可以保证它在每个浏览器上都能正常工作。但是正如我之前说过的那样,木偶程序/ GeoKoor还没有完全准备好使用(这是我的观点),所以考虑使用旧的Firefox驱动程序,等待GeCKoRever完全增长。谢谢你的帮助,但我没有得到任何异常sendkeys
操作太快,因此如果我在每个语句之前放置Thread.sleep(1000)
,则会跳过,然后它会按预期填充数据是的,我以前也有这些问题。在测试自动化中,额外的等待时间几乎总是答案。我尝试了你的建议,但没有成功:(.使用Thread.sleep()
method可以