URL not getting passed on parallel execution of methods

598 views
Skip to first unread message

Aswathy

unread,
Jan 14, 2017, 11:22:44 AM1/14/17
to testng-users
Hi ,
I am facing weird issue of URL not getting passed to browser. My test class has multiple @Test 's. Though multiple browser instances and threads are getting created, only one browser instance has URL. In case of chrome , its showing "data:,"
and in firefox empty url.

Kindly find below configuration

public class BaseTest {

protected WebDriver driver ;
protected DesiredCapabilities desiredCapabilities;

HomePage pg;

@BeforeMethod
@Parameters("browser")
public void setUp(String browser) throws MalformedURLException {

System.out.println("in the setup method "+Thread.currentThread().getId());

if (browser.equalsIgnoreCase("firefox")) {
System.setProperty("webdriver.gecko.driver", "src/test/resources/geckodriver");
driver = new FirefoxDriver();
driver.manage().window().maximize();

} else if (browser.equalsIgnoreCase("chrome")) {
System.setProperty("webdriver.chrome.driver", "src/test/resources/chromedriver");

driver = new ChromeDriver();
Toolkit toolkit = Toolkit.getDefaultToolkit();
int Width = (int) toolkit.getScreenSize().getWidth();
int Height = (int) toolkit.getScreenSize().getHeight();
driver.manage().window().setSize(new org.openqa.selenium.Dimension(Width, Height));


} else {
throw new IllegalArgumentException("Invalid browser value!!");
}
driver.get("http://automationpractice.com/index.php");

}

@AfterMethod
public void teardown() throws Exception {
driver.quit();
}

TESTNG.XML

<suite name="TestNG Test" parallel="methods" thread-count="4">
<test name="Chrome Tests">

<classes>
<parameter name="browser" value="chrome" />
<class name="com.abn.testNGTest.FirstTest"/>
</classes>
</test>

</suite>


public class FirstTest extends BaseTest {

@Test()
public void aTest() throws IOException {

System.out.println("aTest in " + getClass().getSimpleName()
+ " with Thread Id: " + Thread.currentThread().getId());
pg = new HomePage(driver);
}


@Test()
public void bTest() throws Exception {
System.out.println("contactUsTest in " + getClass().getSimpleName()
+ " with Thread Id: " + Thread.currentThread().getId());
pg = new HomePage(driver);

}



Here only one of the test method gets URL though multiple threads and browser instance gets created.

Kindly let me know if am doing something wrong in configuration.

Regards,

Aswathy

Krishnan

unread,
Jan 16, 2017, 11:10:31 PM1/16/17
to testng-users
Aswathy,

Your code seems to be suffering from concurrency issues.
Here's a threadsafe version of what you are trying to do.


public class GetParamsFromSuite {
    private static ThreadLocal<String> names = new ThreadLocal<>();

    @BeforeMethod
    @Parameters ("name")
    public void setup(String name) {
        names.set(name);
    }

    @Test
    public void testMethod() {
        System.err.println("Obtained : " + names.get());
        Assert.assertNotNull(names.get(), "Ensuring name is never null.");
        Assert.assertTrue(names.get().contains("tiger"));
    }

    @Test
    public void anotherTestMethod() {
        System.err.println("Obtained : " + names.get());
        Assert.assertNotNull(names.get(), "Ensuring name is never null.");
        Assert.assertTrue(names.get().contains("lion"));
    }

    @AfterMethod
    public void cleanup() {
        names.set(null);
    }
}

Here's a suite xml file


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="1265_Suite" parallel="methods" verbose="2">
    <test name="92">
        <classes>
            <class name="org.rationale.emotions.GetParamsFromSuite">
                <methods>
                    <include name="testMethod">
                        <parameter name="name" value="tiger clan"/>
                    </include>
                    <include name="anotherTestMethod">
                        <parameter name="name" value="lion clan"/>
                    </include>
                </methods>
            </class>
        </classes>
    </test>
</suite>
Here's the output

...
... TestNG 6.9.10 by Cédric Beust (ced...@beust.com)
...

[TestNG] Running:
  /Users/krmahadevan/playground/scrappable-project/src/test/resources/get_params_from_suite.xml
[TestRunner] Starting executor for test 92 with time out:2147483647 milliseconds.
Obtained : tiger clan
Obtained : lion clan
PASSED: testMethod
PASSED: anotherTestMethod

===============================================
    92
    Tests run: 2, Failures: 0, Skips: 0
===============================================

===============================================
1265_Suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================
Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/

Aswathy

unread,
Jan 17, 2017, 1:25:00 AM1/17/17
to testng-users
Hi Krishnan,

Thanks a lot. Meanwhile i have gone through one of your blog on the same : https://rationaleemotions.wordpress.com/2013/07/31/parallel-webdriver-executions-using-testng/

This helped in solving the issue.

Thank you.

Regards,

Aswathy
Reply all
Reply to author
Forward
0 new messages