简体   繁体   English

自定义Selenium Java Junit4 Webdriver导出器

[英]Customising the Selenium Java Junit4 Webdriver exporter

I have done some research and a lot of mucking around in selenium IDE and I cannot find any way of doing this yet. 我已经在selenium IDE中进行了一些研究和大量的研究,但我还没有找到任何方法。

What I am trying to do is basically add extra support and change the existing implementation for some the commands, namely isTextPresent and addSelection. 我要做的是基本上添加额外的支持并更改一些命令的现有实现,即isTextPresent和addSelection。 I basically can leave most of the functionality of the WebDriver JUnit exporter alone as it is working fine but just want to added some customised method returns. 我基本上可以单独留下WebDriver JUnit导出器的大部分功能,因为它工作正常但只是想添加一些自定义方法返回。

For example I would like to change the out of the exporter for isTextPresent() from this: 例如,我想从此更改isTextPresent()的导出器:

// Warning: waitForTextPresent may require manual changes
for (int second = 0;; second++) {
    if (second >= 60) fail("timeout");
     try { if (driver.findElement(By.cssSelector("BODY")).getText().matches("^[\\s\\S]*SOME INTERESTING TEXT[\\s\\S]*$")) break; } catch (Exception e) {}
            Thread.sleep(1000);
}

To this: 对此:

SeleniumHelperUtil.isTextPresent("SOME INTERESTING TEXT");

This is so I can use my customised SeleniumHelperUtil java class that I want all my selenium tests to use. 这样我就可以使用我希望所有selenium测试使用的自定义SeleniumHelperUtil java类。 This is large complex workflow project so we will end up having hundreds of tests. 这是一个庞大的复杂工作流项目,所以我们最终会有数百个测试。 I would like my testers to build their testcases, export them into Java using the WebDriver into JUnit4 tests. 我希望我的测试人员构建他们的测试用例,使用WebDriver将它们导出到Java中进入JUnit4测试。 Then they can check them into CVS where our automatic Hudson build server will run the new tests nightly. 然后他们可以将它们检入CVS,我们的自动Hudson构建服务器将在每晚运行新的测试。 I would like this to happen with minimal intervention from our Devs (well for now it is only me ATM really and I don't have the time until they invent a 30 hour day :)). 我希望这可以在我们开发人员的干预最少的情况下发生(现在它只是我的ATM真的,我没有时间,直到他们发明了30小时的一天:))。

What I have tried 我试过了什么

I have tried making a custom exporter by doing a cut and paste of all the code from webdriver.js, including the options, and I have modified the waitFor function to look like this: 我尝试通过剪切和粘贴webdriver.js中的所有代码(包括选项)来创建自定义导出器,并且我已将waitFor函数修改为如下所示:

function waitFor(expression) {
  return "SeleniumHelperUtil.isTextPresent(" +  expression ")";
}

Unfortunately all I am getting back is the WebDriver.js implementation which is: 不幸的是,我要回的是WebDriver.js实现,它是:

// Warning: waitForTextPresent may require manual changes
for (int second = 0;; second++) {
    if (second >= 60) fail("timeout");
     try { if (driver.findElement(By.cssSelector("BODY")).getText().matches("^[\\s\\S]*SOME INTERESTING TEXT[\\s\\S]*$")) break; } catch (Exception e) {}
            Thread.sleep(1000);
}

I am using version 1.9.1 of Selenium and Firefox 13 (on a thinapp implementation because of work restrictions I cannot have the full installed version apparently). 我正在使用Selenium和Firefox 13的1.9.1版本(在thinapp实现上,因为工作限制我显然不能完全安装版本)。

Now I have found the part of the code in WebDriver-Junit4.js file in the xpi file so I could modify it and rebuild the plugin with my custom code, but it would be nice just to do it via the selenium-ide as I am not sure what else I might bust when I am in there. 现在我已经在xpi文件中的WebDriver-Junit4.js文件中找到了代码的一部分,所以我可以修改它并使用我的自定义代码重建插件,但是通过selenium-ide来做它会很好我不知道当我在那里时还有什么可能会破坏。

public boolean isTextPresent(WebDriver driver, String textToCheck)
{
    try
    {
        org.openqa.selenium.By by = By.xpath("//p[contains(.,'"+textToCheck+"')]"));
        driver.findElement(by); 
        return true;
    }
    catch (NoSuchElementException e)
    {
        return false;
    }
}

To check the result of the Selenium Unit Testing : use firebug. 要检查Selenium Unit Testing的结果:使用firebug。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM