from selenium test scripts back to selenese

2019-05-31 10:19发布

问题:

I've got a bunch of test scripts written using Test::WWW::Selenium (but the fact that I used perl is inconsequential, any language selenium supports probably has the problem I describe). When the cogs in my application come loose I'd like to replay the test using the Selenium IDE, letting me have full control over the action

But I'm not sure how to go from my test script back into the selenium IDE, you can't paste perl into the IDE and have it transmogrify back to "HTML" (aka, internal selenium language, aka selenese). It was easy enough getting it out, alas.

The RemoteRunner has the command history in it, and I can copy and paste from this directly into selenium ide, and that would be great, but the history box will only hold 5 or 6 lines and clears itself! So it taunts me and is useless.

So how to log these more effectively? The logging options for the selenium rc (-browserSideLog, -log) don't seem to be helpful in this regard. I'm thinking of finding its RemoteRunner.html and hacking at it maybe so it won't clear that list, but is there another way?

回答1:

It seems to be a not very common task but very interesting one. I do not think that there's some standard way so something has to be implemented.

I do not know Perl much and I use Java, so this is just a heads up:

I would extend the DefaultSelenium class for my tests that will use extended HttpCommandProcessor that will logs all commands performed:

import com.thoughtworks.selenium.HttpCommandProcessor;

public class ExtHttpCommandProcessor extends HttpCommandProcessor {

    public ExtHttpCommandProcessor(String serverHost, int serverPort,
            String browserStartCommand, String browserURL) {
        super(serverHost, serverPort, browserStartCommand, browserURL);
    }

    public String doCommand(String commandName, String[] args) {
        StringBuffer sb = new StringBuffer();

        sb.append("|" + commandName + "|");     

        if (args!=null) {
            for (String arg : args) {
                sb.append(arg + "|");
            }

            if (args.length<2) {
                sb.append(" |");
            }
        } else {
            sb.append(" | |");
        }

        System.out.println(sb.toString());
        // or log it where you want

        return super.doCommand(commandName, args);
    }

}

And

import com.thoughtworks.selenium.DefaultSelenium;

public class ExtSelenium extends DefaultSelenium {
    public ExtSelenium(String serverHost, int serverPort,
            String browserStartCommand, String browserURL) {
        super(new ExtHttpCommandProcessor(serverHost, serverPort, browserStartCommand, browserURL));
    }
}

Then I would extend SeleneseTestCase for use as the base in my tests:

import com.thoughtworks.selenium.SeleneseTestCase;

public class ExSeleneseTestCase  extends SeleneseTestCase {    

    public void setUp(String url, String browserString) throws Exception {
        int port = 4444;
        if (url==null) {
            url = "http://localhost:" + port;
        }
        selenium = new ExtSelenium("localhost", port, browserString, url);
        selenium.start();
        selenium.setContext(this.getClass().getSimpleName() + "." + getName());
    }

}

The output of such test will look like:

|getNewBrowserSession|*iexplore|http://localhost:8080/|
|setContext|SimpleTest.testNew| |
|open|/webapp/test.html| |
|isTextPresent|Sample text| |
|click|sampleLink| |
|waitForPageToLoad|10000| |
|testComplete| | |

This solution will not log verifys and asserts so they may also be overrided in ExSeleneseTestCase to produce some trace.



回答2:

You could also go the other way, which is probably easier: save the (partial) selenese HTML files when you generate them, and from the perl/java/... code, parse the files and execute the lines using doCommand. That way, you don't have to reconstruct the asserts/verify statements, because they weren't lost in the first place.