开发者

Selenium, minimizing repetitious code

The selenium tests I'm gonna be doing are basically based on three main steps, with different parameters. These parameters are passed in from a text file to the test. this allows easy completion of a test such as create three of "X" without writing the code to do the create three times in one test.

Imagine i have a test involving creating two of "X" and one of "Y". CreateX and CreateY are already defined in separate tests. Is there a nice way of calling the code contained in createX and createY from say, Test1?

I tried creating a class with the creates as seperate methods, but got errors on all the selenium.-anything-, ie every damn line. it goes away if i extend seleneseTestCase, but it seems that my other test classes wont import from a class that extends seleneseTestCase. I'm probably doing something idiotic but i might as well ask!

EDIT: well for example, its gonna be the same setUp method for every test, so id like to only write that once... instead of a few hundred times...

public void ready() throws Exception 
{
    selenium = new DefaultSelenium("loca开发者_开发百科lhost", 4444, "*chrome", "https://localhost:9443/");
    selenium.start();
    selenium.setSpeed("1000");
    selenium.setTimeout("999999");
    selenium.windowMaximize();
}

thats gonna be used EVERYWHERE.

its in a class called reuseable. Id like to just call reuseable.ready(); from the tests SetUp... but it wont let me....


public class ExampleTest {
    @Before
    public void setup() {
        System.out.println("setup");
    }

    public void someSharedFunction() {
        System.out.println("shared function");
    }

    @Test
    public void test1() {
        System.out.println("test1");
        someSharedFunction();
    }

    @Test
    public void test2() {
        System.out.println("test2");
        someSharedFunction();
    }
}

The contents of the function after the @Before annotation is what will be executed before every test. someSharedFunction() is an example of a 'reusable' function. The code above will output the following:

setup
test1
shared function
setup
test2
shared function


I would recommend using JUnit and trying out some of the tutorials on junit.org. The problem you have described can be fixed using the @Before annotation on a method that performs this setup in a super class of your tests

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜