4

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 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

You missunderstood my question. I don't want to run the init code before the class:) Let's suppose we have a long init. What I want to do, i Write the init in one testcase, do the test, but don't close the browser window and reuse the session. So when I run my other test MyTestUsesInitsSession, it connects to the already opened session, and continues the work done. It's like when you try to see if a knife cuts different things. You build the table once, and only change the knifes and the cuttable things. Sorry for the stupid example:)
With respect, I think you misunderstood my answer :-) because it provides exactly what you are asking for. I suspect you are getting thrown off by the terminology. Think of the class as the container for all the tests that you want to run after your long init and designated methods in this class are the individual tests. The init runs, then all the tests run without rerunning the init again. Do ask more questions if further clarification needed.
Hmm, still misunderstanding:). I am building tests, so testing test, developing tests. I want to run one test after the init. Because I am building that one test. So one time I wanna run it witha a "click here", and if on the next run, I change my code to "click that" and I want to run it again, without rerunning the initialization code.
OK, I was misunderstanding.:-) I thought you had several tests to run together with a single initialization. Alas, my solution does not cover your situation.
0

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.

5 Comments

What I want to do is test creation, and not test running. Meaning I would use this sessionreuse when developing tests, not when actually running them. Just for debug.
You can use it during development of tests. Why don't you want to use it during execution of tests?
I am using this, but this is not enough. Did you read my other comment, just above?
I did indeed :), though not sure why you don't want to use it while running tests. Following the comments I mentioned above you could indeed reuse session. Whether you want to use it during debugging or execution of test.
Testng is used, only that I want to speed up debugging of the test creations. I want to create an initialized state of the browser and webapp, and i want to get this selenium session from the server, every time i run a test.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.