开发者

Selenium RC User Defined Functions

开发者 https://www.devze.com 2023-01-16 10:07 出处:网络
Trying to do something simple - I have a set of statements to clear browser cookies: public void clearCookies () {

Trying to do something simple - I have a set of statements to clear browser cookies:

public void clearCookies () {
     selenium.open("http://www.myurl开发者_JAVA技巧.com");
     selenium.waitForPageToLoad("10000");
     selenium.deleteAllVisibleCookies();
    }

Now, if I use this function in a test script (using TestNG), calls to this work perfectly. However, if I moved this function to a separate class and change the declaration to include "static", the "selenium" keyword is not recognized.

In a configuration class (say configClass),

public static void clearCookies () {
     selenium.open("http://www.myurl.com");
     selenium.waitForPageToLoad("30000");
     selenium.deleteAllVisibleCookies();
    }

Now, in my test script, if I call configClass.clearCookies();, I get a runtime error I tried declaring DefaultSelenium selenium = new DefaultSelenium(null);, in the clearCookies() function, but that too results in a runtime error.

I do have the import com.thoughtworks.selenium.*; import in my configClass.

Any pointers would be appreciated. Thanks.


You can do two things.

Refer to the same selenium object in both the classes i.e. in configClass and the class you are calling configClass.clearCookies().

or else

send selenium object to the clearCookies. So the code would be like this

public static void clearCookies (DefaultSelenium selenium) {

 selenium.open("http://www.myurl.com");
 selenium.waitForPageToLoad("30000");
 selenium.deleteAllVisibleCookies();

}

0

精彩评论

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