简体   繁体   中英

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.

What I am trying to do is basically add extra support and change the existing implementation for some the commands, namely isTextPresent and 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.

For example I would like to change the out of the exporter for isTextPresent() from this:

// 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. 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. Then they can check them into CVS where our automatic Hudson build server will run the new tests nightly. 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 :)).

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:

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

Unfortunately all I am getting back is the WebDriver.js implementation which is:

// 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).

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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