简体   繁体   中英

TestNG, Selenium How to share object among multiple Classes

I am executing my test cases using Selenium + TestNG. My test cases are spread across multiple classes. I want to use same copy of webDriver with every class. (I dont want to login every time and then do other operations so option of invoking separate browser each class is gone). To solve this problem, right now I am using inheritance with static webdriver object. But While using selenium grid with parallel mode, it does not work as it uses same object. Can you please show me a way in which, I can share the object with multiple class OR To create a different instance of static on each "test" (its like, I am running separate Java program for each test).

Currently you are using static reference of webdriver object across various classes/tests. So, there is single instance of driver that is modifying the static resources in your code. But when you go for grid, you will create different driver instance for each node. These drivers are going to run in parallel and will be accessing shared resources(static variables, etc.) simultaneously changing and overriding changes made by other driver instances which will cause issues.

Write your code such that resources are not shared(min. use of static). Try passing your driver instance to methods to ensure that required driver is only executing that piece of code.

Here is how I have implemented the solution.

I have an object myObj, which has webDriver object inside it and few more things. I have created static array of static object myObj. Its like this

// some part of class which has WebDriver implemented in it. WebDriver is not static btw.

private static MyClass[] ObjectArr = new MyClass[100];

public static MyClass getInstance(Integer id){
    id = (id==null)? 0 : id;
    if(ObjectArr[id] == null){
        ObjectArr[id] = new MyClass();
    }  
    return ObjectArr[id];
}

Now, when I want to use this in my testng class, I do it like this.

public class x{
MyClass i;

@BeforeClass
@Parameters("instanceId")
public void getInstance(@Optional String instanceId){
    i = MyClass.getInstance(Integer.parseInt(instanceId));
}
}

and Here is my testng.xml looks like.

<test name="FireFox" preserve-order="true">
    <parameter name="instanceId" value="1" />
    <parameter name="browser" value="firefox" />
    <classes>
        <class name="com.app.Login"/>
    </classes>
</test> 

<test name="Chrome" preserve-order="true">
    <parameter name="instanceId" value="2" />
    <parameter name="browser" value="chrome" />
    <classes>
        <class name="com.app.Login"/>
    </classes>
</test> 

This works perfectly for me for selenium grid in parallel and in normal way.

I ran into similar problem. Here is what I did.

Assumption: Say Class B tests depend upon the Class A instance.

@AfterClass(alwaysRun = true)
public void insertValueIntoAttribute(ITestContext ctx) {
    ctx.setAttribute("INSTANCE_OF_A", this); // from inside class A tests
}

And then in class B, I retrieve this instance of A and use it.

@BeforeClass(alwaysRun=true)
@SuppressWarnings("unchecked")
public void fetchData(ITestContext ctx) {
    A a = (A) ctx.getAttribute("INSTANCE_OF_A");
}

Hope this helps someone.

We struggled with a similar situation with our testing efforts. We also wanted to leverage the TestNG parallelization along with Selenium web driver. Unfortunately, managing the web driver life cycle within each test class or trying to use a static class holding the web driver wasn't effective when enabling any of the TestNG parallel modes. ie. parallel="tests|classes|methods" . Matching classes and methods under test with the active TestNG thread and the instantiated web driver was unwieldy when it worked at all. We were spending more time figuring out the web driver lifecycles than getting to the real web application tests. We found a better way. We have an open source project that abstracts away the web driver lifecycle management for TestNG/Selenium testing. It properly supports each of the TestNG parallel modes providing the correct and active web driver for each test class method under test. It's helped us greatly in our testing efforts. Perhaps it may be of use in your case as well. Or if nothing else give you an idea of how you might tackle it differently. The project is over on GitHub as Concurrent Selenium TestNG (COSENG) . Kind regards.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM