Session management in Selenium webdriver - selenium

I have a class under which I have different methods one of them is login and other are related to the adding product to cart, checkout and shipping.
When I try to run the methods using TestNG in one go, it execute all the methods in different browsers session and I lost my login session.
I want some solution so that either all the methods execute in same browser or I can use the session of login method in other consecutive methods
Thanks in advance
I have same login for both
I am creating new instance of driver in both methods
Here is my code :
public class purchase {
#Test
public void login(){
System.setProperty("webdriver.chrome.driver", "{path}/chromedriver.exe");
WebDriver fd= new ChromeDriver();
fd.get("{domain}/login/");
/*{login script here}*/
}
#Test
public void purchaseItem(){
System.setProperty("webdriver.chrome.driver", "{path}/chromedriver.exe");
WebDriver fd1= new ChromeDriver();
fd1.get("{domain}/travel");
/*add item to cart*/
}
}

It means there might be 2 issue:
1. You have separate login in both the methods
2. You are creating new instance of driver in both methods.
Please share your code and will help you with resolving it.

You have to share the same driver instance between methods. Otherwise, you will have a new session each time, like you described it.

Here you go.. as per your comment - "I am creating new instance of driver in both methods"..
So, if you want to maintain the session, you have to use a single instance of driver in both methods.
You can achieve it using beforeClass method (Use it for driver initializing and login), and write your logic in different test methods.. So it will make sure complete test cycle for the class is 1 and then your session will not be lost.
Let me know if this helps.

Related

NUnit, Run Parametrized Tests in Parallel with Selenium

I'm having an issue trying to do as the title says. When i run this locally, it does launch 2 chrome instances, however it uses only one of the browsers for both tests, as opposed to using each browser for each test. Any ideas how how to set this up correctly?
public class BaseClass
{
public IWebDriver driver;
[SetUp]
public void BaseSetUp()
{
driver = new ChromeDriver();
driver.Manage().Window.Maximize();
}
[TearDown]
public void BaseTearDown()
{
driver.Quit();
}
}
[Parallelizable(ParallelScope.All)]
[TestFixture]
public class DerivedClass : BaseClass
{
[TestCase("https://www.python.org/", "Welcome to Python.org")]
[TestCase("https://www.w3.org/", "World Wide Web Consortium (W3C)")]
public void Test3(string url, string title)
{
driver.Navigate().GoToUrl(url);
Thread.Sleep(4500);
Assert.AreEqual(driver.Title, title);
}
}
You are indeed creating the driver twice, but you are storing both instances in the same member field, driver. The reason this happens is that NUnit uses the same instance of a test class for all the tests within that class. (A new feature will be available in a future release to use a separate instance, but that's no help to you right now.)
As the tests run in parallel, the first test to run performs its setup and stores the driver in that field. Then the second test starts and stores it's instance in the same field. It's not possible to predict exactly when - during the execution of the tests- that replacement will take place. However, it will most likely happen consistently as you re-run on the same machine.
In the case of the example, the solution is simple. If you want each test case to use a separate driver instance, create the driver within the test itself, perhaps by calling a method that centralizes all your initialization.
An important thing to remember is that the ParallelizableAttribute is used to tell NUnit that the test may be run in parallel. NUnit accepts that as a promise, but if you share state between tests they won't run correctly.

Selenium Webdriver drive two browser

I'm trying to run two distinct actions in two differents browser.
With
Is Selenium WebDriver thread safe? i have learned an important thing: "You /can/ on the other hand instantiate one WebDriver instance for each thread. "
So i have created two thread who instantiate two drivers (first for google, second for yahoo) like this :
public class Thread2 extends Thread{
#Override
public void run() {
File file = new File(Thread2.class.getClassLoader().getResource("chromedriver").getPath());
System.setProperty("webdriver.chrome.driver", file.getPath());
WebDriver driver = new ChromeDriver();
driver.get("http://www.yahoo.com");
while (true) {
driver.findElement(By.id("uh-search-box")).sendKeys("test");
}
}
My Main Class just implementing those two thread and call run function.
There is a way to get two or X browsers running at the same time and excecuting differents task ?
Regards.
More informations at https://github.com/AMimicD/TestSeleniumThread
have you tried to created two different driver variables?
WebDriver driver = new ChromeDriver();
driver.get("http://www.yahoo.com");
WebDriver driver2 = new ChromeDriver();
driver2.get("http://www.whateveryouwant.com");
I assume you want to run your test parallely at the same time. So, in your main method, replace
t1.run();
t2.run();
with
t1.start();
t2.start();
Now you should see both your browsers running at the same time.
Thread.start() is required to actually create a new thread so that the runnable's run method is executed in parallel.
Note: There is a problem with your while condition. The condition is always true and hence it will be an infinite loop. Please fix this in your code.
Integrate your script with TestNG. It will help you to run your script in parallel mode.
Please Refer link to know more about Parallel execution with TestNG.
Thanks!

Webdriver.quit() ,not working properly when called in #AfterSuite annotation

I have a two "test" in testng XML file, all are running parallel what my issue is that I called a Webdriver.quit() method in the #AfterSuite annotation, but it is only closing the last created browser instance not all opened browsers.
when I call in the #AfterTest annotation it gets closed.
below is my Testng file:
I created the driver in the base class and extend it in test classes.
code for creating driver instance.
#BeforeClass
#Parameters({"deviceName","platformName","platformVersion","udid","browserName","browserVersion","nodeIP","nodePort"})
public void startBrowser(String deviceName, String platformName, String platformVersion, String udid, String browserName, String browserVersion, String nodeIP, String nodePort)
{
DesiredCapabilities capabilities =DesiredCapabilities.chrome();
System.setProperty("webdriver.chrome.driver", "chromeDriverPath");
ChromeOptions options = new ChromeOptions();
capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
driver = new ChromeDriver(capabilities);
}
AfterSuite is called once per suite. What you have shown above is one suite executing two tests in parallel. You need to make sure your driver is thread safe.
AfterTest gets called after <test> tag is over - so your webdriver if specific to a thread will get closed.
If you want to close all webdriver in aftersuite, you need to build a list of webdrivers and close each one individually.
Because you have 2 <test>, TestNG is creating 2 instances (with the same after suite method).
But testng consideres a suite method as single in the suite and will only call it once (the first it finds).
That's why it is working with after test method which looks to be the one you should use.
BTW, as you want to close the driver in an after suite method, I suppose you create it in a before suite method which is not supposed to work for the same reasons. Could you detail where/how you create the driver ?

Issue with parallel execution with Selenium Grid for Webdriver + TestNG

I am trying to set up Webdriver parallel execution with Webdriver Hub and TestNG parallel mechanism. I am facing an issue with thread
I have this class which extends TestBaseSetUp, which has a BeforeMethod and AfterMethod and set to run always. For webdriver parallel execution, I wanted to use ThreadLocal, but #Test and #Before/#After method are in different thread.So If I set webdriver as ThreadLocal in my TestBaseSetUp, and try get in my test method it returns null.
public class TestCheck extends TestBaseSetUp {
#Test
public void test(){
System.out.println("Thread in test " + Thread.currentThread().getId());
}
}
Do we have a way so that #Test is also in same thread as #Before/#After method
#Manish_pat
Take a look at this blog post of mine : https://rationaleemotions.wordpress.com/2013/07/31/parallel-webdriver-executions-using-testng/
The idea is to move away from relying on config methods for web driver instantiation into a TestNG listener driven model wherein the webdriver object is created from within the beforeInvocation() and destroyed in the afterInvocation().
TestNG guarantees that beforeInvocation(), #Test and afterInvocation() would always be executed on the same thread. So now you can go ahead and work with ThreadLocal in here.
did you tried
#Test(singleThreaded=true)
At class level. so that all test methods in that class will run on same thread. here is example
Thank You,
Murali

Migrating to Webdriver from Selenium RC

I am migrating from RC to webdriver.
In my existing project I use methods from the Selenium Class like
selenium.click()
selenium.type()
etc.
Do I need to change these to the equivalent webdriver commands, or is there a way i can still use these commands?
I use firefox 12, Eclipse IDE
There is the WebDriverBackedSelenium. Essentially this is a bridge between the RC API and WebDriver API. This will do what you are after, there will be some modification to code, but majority will still be the same. It gives you the flexibility of the WebDriver itself, while keeping old code the same.
It is highly recommended to fully convert your solution to use the WebDriver API directly.
The WebDriver API is constantly being updated, worked on and supported.
The RC API and the "RC-WebDriver-Bridge" (WebDriverBackedSelenium) won't be.
Page on WebDriverBackedSelenium exists here:
http://seleniumhq.org/docs/03_webdriver.html#alternative-back-ends-mixing-webdriver-and-rc-technologies
Sample usage to create a new instance of Firefox:
var driver = new FirefoxDriver();
var selenium = new WebDriverBackedSelenium(driver, baseUrl);
selenium.open("http://www.google.com");
selenium.type("name=q", "cheese");
selenium.click("name=btnG");
selenium.stop();
After creating a WebDriverBackedSelenium instance with a given Driver, one does not have to call start() - as the creation of the Driver already started the session. At the end of the test, stop() should be called instead of the Driver's quit() method.
This is more similar to WebDriver's behaviour - as creating a Driver instance starts a session, yet it has to be terminated explicitly with a call to quit().