【问题标题】:Selenium Script fails on Firefox BrowserSelenium 脚本在 Firefox 浏览器上失败
【发布时间】:2017-06-03 19:56:38
【问题描述】:

我正在使用 -

Selenium 3.0.1 与 java

火狐版本:50.1.0

geckodriver-v0.13.0-win64

我已经为我的 Web 应用程序创建了一套完整的测试套件。当我在chrome 浏览器上运行它时,所有测试都运行顺利。除了浏览器兼容性测试之外完全没有问题,我切换到firefox驱动程序并运行测试,我的测试总是失败。输入字段出现问题,我猜输入文本的速度太快了,即使文本没有被填充到文本框中,但它正在单击提交按钮,因为我能够看到空白字段验证消息。我米吸了如何处理这个。我需要在 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

【问题讨论】:

  • 您能否分享一下您的代码中测试首先失败的部分,以便我可以帮助您?
  • 我读到了“调试脚本”——如果是 JavaScript 相关问题,请更新标签 :)
  • @minhundje,代码已添加,请查看
  • 错误信息是什么?
  • @mosaad,没有错误。但无法填写表格中的数据

标签: java selenium selenium-webdriver geckodriver


【解决方案1】:

我遇到了完全相同的问题。在某些表单上,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);
}

sendkeys send something, but still I have to fill the form

【讨论】:

    【解决方案2】:

    当您遇到问题时,一个好的帮助/解决方案始终是额外的等待时间。为了做到这一点,我建议你使用这样的函数,等待找到一个元素:

    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();
    }
    

    请注意,出于 OOP 的原因,我已将等待函数放在不同的类中。 您可以根据需要更改此代码。

    如果您还有其他问题,我随时为您提供帮助。

    【讨论】:

    • 感谢您的帮助,但我没有收到任何 exceptionsendkeys 动作太快了,所以如果我在每个语句之前加上 Thread.sleep(1000) 就会跳过它,然后它会按预期填充数据
    • 是的,我以前也遇到过这些问题。额外的等待时间几乎总是测试自动化的答案。
    • 我试过你的建议,但没有运气:(。Thread.sleep() 方法很好
    【解决方案3】:

    相同的代码适用于 chrome?根据我的经验,geckodriver 仍然存在此类问题,使其难以使用。如果您无法为此创建解决方法并且无法切换到 chromedriver(或其他驱动程序),我建议您使用“旧”Firefoxdriver 而不是 geckodriver/marionette。

    当然,请在此处发布有问题的代码,以便我们先解决问题。 :)

    【讨论】:

    • 哪里失败了?
    • 没有在文本字段中输入文本,似乎动作太快了,所以它正在跳过。
    • 太快了怎么办?可能点击后需要等待这个字段渲染,有没有找服务员试试?
    • 我已经使用chrome 创建了我的脚本,所以无论我遇到什么问题,我都可以管理等待。现在我想在firefox 上运行相同的套件所以它似乎付出了一些额外的努力来调试FF 上的整个套件并进行相应的修改。假设我这样做了,那么我如何确定它在其他浏览器或以前的浏览器上运行良好?有什么通用的解决方案可以摆脱这个吗?
    • 不幸的是,没有通用的解决方案可以保证它在每个浏览器上都能正常工作。但正如我之前所说,marionette/geckodriver 还没有准备好完全使用(这是我的观点),所以考虑使用旧的 firefox 驱动程序并等待 geckodriver 完全成长。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-11
    • 1970-01-01
    • 1970-01-01
    • 2017-01-10
    • 2022-09-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多