Selenium Test - preserve session across multiple t

2020-03-26 05:11发布

I have the following issue. When I start my selenium test, to get to the part where the actual test is performed, I need to start the browser, log in, do some other operations, and then it comes the part I want to test.

Isn't there a way to do the first part only once, leave the session and the browser open. And for a next test Run only continue this session, without starting up.

So basically I would have a test initializing, and leaving the session open. And other tests which use this initialized session, reuse the session every time.

I am using Java, and Selenium RC.

Thanks!

3条回答
Summer. ? 凉城
2楼-- · 2020-03-26 05:17

In one line: You can use firefox profile for that.

In Detail: 1. Login in your firefox manually (Use, Remember me option of your website if its there. else it should be ok) 2. Go to Firefox Menu > Help > Troubleshooting Information > Profile Folder (Click on it). 3. When you invoke you selenium webdriver (not sure about RC though), use this

ProfilesIni profileIni = new ProfilesIni();
FirefoxProfile profile = profileIni.getProfile("<YOUR PATH TO FF PROFILE FROM STEP 2>");
this.driver = new FirefoxDriver(profile);

This way you'll be directly logged in to your account. This also is faster as many data is picked from browser cache unlike fresh firefox profile

查看更多
地球回转人心会变
3楼-- · 2020-03-26 05:33

I would suggest using testng which lets you use java annotations for pre processing like invocation of certain piece of code only before class or test suite using @BeforeClass, @BeforeSuite respectively. Herein you can define selenium server instantiation, browser invocation to be done either before a class of test suite (there are many other options also available) and then this could be reused in subsequent test methods.

查看更多
男人必须洒脱
4楼-- · 2020-03-26 05:38

It depends a bit upon your environment. I use C# with Selenium RC (and therefore NUnit test framework). Since you use Java (and presumably JUnit) it works the same way. Both JUnit and NUnit let you specify special code that executes before each test, after each test, before each suite, or after each suite.

In your case, you want to supply code to run before a suite (a suite simply being a collection of all your test cases in one namespace (C#) or class (Java)). In C# I use code like this--the [TextFixtureSetUp] directive is what lets NUnit recognize this as suite setup code.

   [TestFixtureSetUp]
    public void SetupTest()
    {
        selenium = Setup.StartSelenium();
        Setup.Login();
        Setup.Preliminaries();
    }

The equivalent designation in Java is to use the @BeforeClass attribute as in

public class Example {
    @BeforeClass
    public static void onlyOnce() {
       ...
    }
 }

My recent article Web Testing with Selenium Sushi: A Practical Guide and Toolset is very focused on C# but it may provide you with some general ideas that you could apply to your Java work.

查看更多
登录 后发表回答