简体   繁体   English

无法在Java上使用@Factory testng批注运行并行测试

[英]Cannot run parallel tests with @Factory testng annotation on java

So I use @Factory to run one test with 5 different emails, but I get wrong number of arguments exception and I can't see the full error trace on console. 因此,我使用@Factory对5封不同的电子邮件运行一项测试,但是我得到了错误数量的参数异常,并且无法在控制台上看到完整的错误跟踪。 I use TestNG. 我使用TestNG。 Here's my code: 这是我的代码:

package com.task.lab.facadetask;

public class GmailTest {
    private WebDriver driver;
    private static List<User> usersList;
    static List<TestMessage> mess;

    public GmailTest(){}


    @Factory(dataProviderClass = GmailTest.class, dataProvider = "getData")
    public GmailTest(WebDriver driver,List<User> usersList, List<TestMessage> mess ){
        this.driver = driver;
        GmailTest.usersList = usersList;
        GmailTest.mess = mess;
    }

    @BeforeMethod
    public void setUpDriver(){
        driver = DriverObject.getDriver();
    }

    @DataProvider
    public static Object[][] getData() {
        File usersXml = new File("src\\\\main\\\\java\\\\com\\\\task\\\\lab\\\\facadetask\\\\testdata\\\\users.xml");

        try {
            usersList = JAXB.unmarshal(usersXml);
        } catch (JAXBException e) {
            e.printStackTrace();
        }
        TestMessages messages =  UnMarshell.unmarshaller();
        assert messages != null;
        mess = messages.getTestMessages();
        return new Object[][]{
                {mess.get(0), usersList.get(0)},
                {mess.get(1), usersList.get(1)},
                {mess.get(2), usersList.get(2)},
        };
    }

    @Test
    public void testGmail(TestMessage message, User users) {
        String gmailURL = "https://accounts.google.com/signin";
        driver.get(gmailURL);
        Login loginPage = new Login();
        loginPage.login(users.getEmail(), users.getPassword());
        GmailMessage gmailPage = new GmailMessage();
        gmailPage.sendMessage(message.getReceiver(), message.getSubject(), message.getMessage());
        gmailPage.removeMessage();
        Assert.assertTrue(gmailPage.isRemoved());
    }
    @AfterMethod
    public void quitBrowser(){
        try{
            driver.close();
        }finally{
            driver.quit();
        }
    }
}

My assumption is that it could be caused by changing the original non static lists of users and messages to static, but DataProvider method needs to be static. 我的假设是,这可能是由于将用户和消息的原始非静态列表更改为静态而引起的,但是DataProvider方法必须是静态的。 Could someone guide me on what am I doing wrong? 有人可以指导我做错什么吗? UPD:So, I removed @BeforeMethod and included driver in @DataProvider as Krishnan suggested but it gives me the same error, wrong number of arguments. UPD:因此,我按照Krishnan的建议删除了@BeforeMethod,并将驱动程序包含在@DataProvider中 ,但它给了我同样的错误,错误的参数数量。 Here's what the DataProvider starts with for now: 这是DataProvider现在开始的内容:

@DataProvider
public static Object[][] getData() {
    driver = DriverObject.getDriver();
    File usersXml = new File   //The rest remains the same

Also, I tried to initialize driver in BeforeMethod but in this case Test doesn't see it. 另外,我尝试在BeforeMethod中初始化驱动程序,但在这种情况下Test看不到它。 It looks like this: 看起来像这样:

@BeforeMethod
public void setUpDriver(){
    WebDriver driver = DriverObject.getDriver();
}

Maybe someone can provide me with a valid analogue of Factory so I can run 5 parallel tests simultaniously? 也许有人可以为我提供Factory的有效模拟,以便我可以同时运行5个并行测试? I am open for suggestions. 我愿意征求意见。

Your factory method is defined to accept 3 arguments. 您的工厂方法定义为接受3个参数。

@Factory(dataProviderClass = GmailTest.class, dataProvider = "getData")
public GmailTest(WebDriver driver,List<User> usersList, List<TestMessage> mess ){
    this.driver = driver;
    GmailTest.usersList = usersList;
    GmailTest.mess = mess;
}

Your data provider is only providing 2 parameters. 您的数据提供商仅提供2个参数。 WebDriver is not being provided by your data provider. 您的数据提供商未提供WebDriver。

You can do one of the following : 您可以执行以下操作之一:

  • Either enhance your data provider to include the WebDriver object via a call to DriverObject.getDriver() and remove your @BeforeMethod method (or) 通过调用DriverObject.getDriver()增强数据提供程序以包含WebDriver对象,并删除@BeforeMethod方法(或)。
  • Alter your constructor's signature to not accept a WebDriver instance, but initialize the class's WebDriver instance via the @BeforeMethod . 更改构造函数的签名以不接受WebDriver实例,而是通过@BeforeMethod初始化类的WebDriver实例。

That should fix your problem. 那应该解决您的问题。

EDIT: The question has been updated. 编辑:问题已更新。 So updating my answer as well. 所以也更新我的答案。

Looking at the updates to the question, the answer is still the same on a high level. 从问题的最新情况看,答案在很大程度上还是一样的。 Now including a sample as well, which explains the answer. 现在还包括一个样本,解释了答案。

Mocking how the User class could look like 模拟User类的外观

import java.util.ArrayList;
import java.util.List;

public class User {
    private String name;

    public User(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public static List<User> newUsers(String... names) {
        List<User> users = new ArrayList<>();
        for (String name : names) {
            users.add(new User(name));
        }
        return users;
    }
}

Mocking how the TestMessage class could look like 模拟TestMessage类的外观

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class TestMessage {
    private String text;

    public TestMessage(String text) {
        this.text = text;
    }

    public String getText() {
        return text;
    }

    public static List<TestMessage> newMessages(int howMany) {
        List<TestMessage> msgs = new ArrayList<>();
        for (int i = 0; i < howMany; i++) {
            msgs.add(new TestMessage(UUID.randomUUID().toString()));
        }
        return msgs;
    }
}

Here's how the test class should look like 这是测试类的样子

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Factory;
import org.testng.annotations.Test;

import java.util.Collections;
import java.util.List;

public class GmailTest {
    private WebDriver driver;
    private List<User> users;
    private List<TestMessage> testMessages;

    @Factory(dataProvider = "getData")
    public GmailTest(WebDriver driver, List<User> users, List<TestMessage> testMessages) {
        this.driver = driver;
        this.users = users;
        this.testMessages = testMessages;
    }

    @Test
    public void testMethod() {
        Assert.assertNotNull(driver);
        Assert.assertNotNull(users);
        Assert.assertNotNull(testMessages);
    }

    @AfterClass
    public void cleanupDrivers() {
        if (driver != null) {
            driver.quit();
        }
    }

    @DataProvider(name = "getData")
    public static Object[][] getData() {
        List<User> users = User.newUsers("Jack", "Daniel", "John");
        int size = users.size();
        List<TestMessage> testMessages = TestMessage.newMessages(size);
        Object[][] data = new Object[size][1];
        for (int i = 0; i < size; i++) {
            data[i] = new Object[]{new FirefoxDriver(), Collections.singletonList(users.get(i)),
                    Collections.singletonList(testMessages.get(0))};
        }
        return data;
    }
}

Here's the execution logs 这是执行日志

1518271888011   geckodriver INFO    geckodriver 0.19.1
1518271888131   geckodriver INFO    Listening on 127.0.0.1:14727
1518271888627   mozrunner::runner   INFO    Running command: "/Applications/Firefox.app/Contents/MacOS/firefox-bin" "-marionette" "-profile" "/var/folders/mj/81r6v7nn5lqgqgtfl18spfpw0000gn/T/rust_mozprofile.5mkpumai11hO"
1518271889362   Marionette  INFO    Enabled via --marionette
2018-02-10 19:41:30.336 plugin-container[53151:969522] *** CFMessagePort: bootstrap_register(): failed 1100 (0x44c) 'Permission denied', port = 0xad33, name = 'com.apple.tsm.portname'
See /usr/include/servers/bootstrap_defs.h for the error codes.
1518271890773   Marionette  INFO    Listening on port 52891
1518271890793   Marionette  WARN    TLS certificate errors will be ignored for this session
Feb 10, 2018 7:41:30 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
1518271890961   geckodriver INFO    geckodriver 0.19.1
1518271891060   geckodriver INFO    Listening on 127.0.0.1:6639
2018-02-10 19:41:31.225 plugin-container[53152:969613] *** CFMessagePort: bootstrap_register(): failed 1100 (0x44c) 'Permission denied', port = 0xaa37, name = 'com.apple.tsm.portname'
See /usr/include/servers/bootstrap_defs.h for the error codes.
1518271891259   mozrunner::runner   INFO    Running command: "/Applications/Firefox.app/Contents/MacOS/firefox-bin" "-marionette" "-profile" "/var/folders/mj/81r6v7nn5lqgqgtfl18spfpw0000gn/T/rust_mozprofile.npquNnysdwGI"
1518271891832   Marionette  INFO    Enabled via --marionette
2018-02-10 19:41:32.786 plugin-container[53155:969741] *** CFMessagePort: bootstrap_register(): failed 1100 (0x44c) 'Permission denied', port = 0xab3f, name = 'com.apple.tsm.portname'
See /usr/include/servers/bootstrap_defs.h for the error codes.
1518271893243   Marionette  INFO    Listening on port 53150
1518271893342   Marionette  WARN    TLS certificate errors will be ignored for this session
Feb 10, 2018 7:41:33 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
1518271893499   geckodriver INFO    geckodriver 0.19.1
1518271893590   geckodriver INFO    Listening on 127.0.0.1:48408
2018-02-10 19:41:33.681 plugin-container[53156:969822] *** CFMessagePort: bootstrap_register(): failed 1100 (0x44c) 'Permission denied', port = 0x7c37, name = 'com.apple.tsm.portname'
See /usr/include/servers/bootstrap_defs.h for the error codes.
1518271893810   mozrunner::runner   INFO    Running command: "/Applications/Firefox.app/Contents/MacOS/firefox-bin" "-marionette" "-profile" "/var/folders/mj/81r6v7nn5lqgqgtfl18spfpw0000gn/T/rust_mozprofile.65SomKttNwQP"
1518271894377   Marionette  INFO    Enabled via --marionette
2018-02-10 19:41:35.326 plugin-container[53159:969958] *** CFMessagePort: bootstrap_register(): failed 1100 (0x44c) 'Permission denied', port = 0x1523b, name = 'com.apple.tsm.portname'
See /usr/include/servers/bootstrap_defs.h for the error codes.
1518271895785   Marionette  INFO    Listening on port 53451
1518271895824   Marionette  WARN    TLS certificate errors will be ignored for this session
Feb 10, 2018 7:41:35 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
[GFX1-]: Receive IPC close with reason=AbnormalShutdown
1518271896172   addons.xpi  WARN    Exception running bootstrap method shutdown on activity-stream@mozilla.org: [Exception... "Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [nsIObserverService.removeObserver]"  nsresult: "0x80004005 (NS_ERROR_FAILURE)"  location: "JS frame :: resource://activity-stream/lib/SnippetsFeed.jsm :: uninit :: line 125"  data: no] Stack trace: uninit()@resource://activity-stream/lib/SnippetsFeed.jsm:125 < onAction()@resource://activity-stream/lib/SnippetsFeed.jsm:141 < _middleware/</<()@resource://activity-stream/lib/Store.jsm:51 < Store/this[method]()@resource://activity-stream/lib/Store.jsm:30 < uninit()@resource://activity-stream/lib/Store.jsm:153 < uninit()@resource://activity-stream/lib/ActivityStream.jsm:278 < uninit()@resource://gre/modules/addons/XPIProvider.jsm -> jar:file:///Applications/Firefox.app/Contents/Resources/browser/features/activity-stream@mozilla.org.xpi!/bootstrap.js:80 < shutdown()@resource://gre/modules/addons/XPIProvider.jsm -> jar:file:///Applications/Firefox.app/Contents/Resources/browser/features/activity-stream@mozilla.org.xpi!/bootstrap.js:196 < callBootstrapMethod()@resource://gre/modules/addons/XPIProvider.jsm:4406 < observe()@resource://gre/modules/addons/XPIProvider.jsm:2270 < GeckoDriver.prototype.quit()@driver.js:3381 < despatch()@server.js:560 < execute()@server.js:534 < onPacket/<()@server.js:509 < onPacket()@server.js:508 < _onJSONObjectReady/<()@transport.js:500

===============================================
Default Suite
Total tests run: 3, Failures: 0, Skips: 0
===============================================


Process finished with exit code 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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