Parllel execution with in @Test annotations

2019-03-05 06:23发布

问题:

I want the actions(switch to frame, NavigateToAgents and writeToExcel) to be performed on multiple instances that open from Webdriver.get(urls). for now it just runs rest of the actions(switch to frame, NavigateToAgents and writeToExcel) on 1st instance of browser that is opened but not on others.

@Test(dataProvider = "data")
public static void stopAgents(String urls) throws Exception {

    setup();    //Setup of browser

    WebDriver.get(urls); // opening multiple instances of webdriver from list of urls from dataprovider

    switchToLeftFrame();

    navigateToAgents();

    writeToExcel(sheetName, methodType); //i want to write to excel the tables of both the instances which are opened

//dataprovider passes list of urls

   @DataProvider(name = "data", parallel = true)
    public Object[][] data() {

    Configuration configuration = Configuration.getConfiguration();

    List<String> urls = configuration.getListOfUrls();

    Object[][] data = new Object[urls.size()][1];

    for (int i = 0; i < data.length; i++) {
        data[i][0] = urls.get(i);
    }

    return data;
}

// TestNG XML

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> 
<suite name="Suite" data-provider-thread-count="2">
<test thread-count="5" name="Test">
<classes>
  <class name="testng.data.StopAgents"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->

Error i get when i run above code

org.openqa.selenium.json.JsonException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
Build info: version: '3.12.0', revision: '7c6e0b3', time: '2018-05-08T14:04:26.12Z'
System info: host: 'LH7U05CG7370KZ5', ip: '10.195.232.34', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_51'
Driver info: driver.version: RemoteWebDriver
    at org.openqa.selenium.json.JsonInput.execute(JsonInput.java:172)
    at org.openqa.selenium.json.JsonInput.beginObject(JsonInput.java:103)
    at org.openqa.selenium.json.MapCoercer.lambda$apply$1(MapCoercer.java:64)
    at org.openqa.selenium.json.MapCoercer$$Lambda$105/1929913939.apply(Unknown Source)
    at org.openqa.selenium.json.JsonTypeCoercer.lambda$null$6(JsonTypeCoercer.java:142)
    at org.openqa.selenium.json.JsonTypeCoercer$$Lambda$109/1456010139.apply(Unknown Source)
    at org.openqa.selenium.json.JsonTypeCoercer.coerce(JsonTypeCoercer.java:122)
    at org.openqa.selenium.json.Json.toType(Json.java:62)
    at org.openqa.selenium.json.Json.toType(Json.java:52)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:87)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:543)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:600)
    at org.openqa.selenium.remote.RemoteWebDriver$RemoteWebDriverOptions$RemoteWindow.maximize(RemoteWebDriver.java:828)
    at testng.data.MethodsProgress.setup(MethodsProgress.java:450)
    at testng.data.parllel.stopAgents(parllel.java:15)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
    at org.testng.internal.TestMethodWithDataProviderMethodWorker.call(TestMethodWithDataProviderMethodWorker.java:73)
    at org.testng.internal.TestMethodWithDataProviderMethodWorker.call(TestMethodWithDataProviderMethodWorker.java:14)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
    at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:385)
    at org.openqa.selenium.json.JsonInput$$Lambda$93/585885412.execute(Unknown Source)
    at org.openqa.selenium.json.JsonInput$VoidCallable.call(JsonInput.java:181)
    at org.openqa.selenium.json.JsonInput$VoidCallable.call(JsonInput.java:176)
    at org.openqa.selenium.json.JsonInput.execute(JsonInput.java:168)
    ... 32 more

回答1:

You need to pass parallel="tests" in your XML file.

For reference read this document