在@Test批注中并行执行

时间:2018-07-26 17:05:58

标签: selenium testng dataprovider

我希望在从Webdriver.get(urls)打开的多个实例上执行操作(切换到框架,NavigateToAgents和writeToExcel)。目前,它仅在打开的浏览器的第一个实例上运行其余动作(切换到框架,NavigateToAgents和writeToExcel),但在其他浏览器上不运行。

@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传递网址列表

   @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 -->

运行上述代码时出现错误

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 个答案:

答案 0 :(得分:0)

您需要在XML文件中通过 parallel =“ tests”

For reference read this document