selenium 异常:测试类应该恰好有一个公共构造函数

qc6wkl3g  于 2022-11-10  发布在  其他
关注(0)|答案(4)|浏览(112)

我已经进行了JUNIT测试

package test.java.com.unit;
import com.saucelabs.common.SauceOnDemandAuthentication;
import com.saucelabs.common.SauceOnDemandSessionIdProvider;
import com.saucelabs.junit.Parallelized;
import com.saucelabs.junit.SauceOnDemandTestWatcher;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.net.URL;
import java.util.LinkedList;

import static org.junit.Assert.assertEquals;

@RunWith(Parallelized.class)
public class WebDriverTest implements SauceOnDemandSessionIdProvider {

public SauceOnDemandAuthentication authentication = new SauceOnDemandAuthentication("USER_NAME", "ACCESS_KEY");

private String browser;
private String os;
private String version;

public @Rule SauceOnDemandTestWatcher resultReportingTestWatcher = new SauceOnDemandTestWatcher(this, authentication);

public @Rule TestName testName = new TestName();

private WebDriverTest(String os, String version, String browser) {
 super();
 this.os= os;
 this.version= version;
 this.browser= browser;
}

@SuppressWarnings({ "unchecked", "rawtypes" })
@Parameterized.Parameters
public static LinkedList browsersStrings() throws Exception {
LinkedList browsers = new LinkedList();
 browsers.add(new String[]{Platform.XP.toString(), "26", "firefox"});
 browsers.add(new String[]{Platform.WIN8.toString(), "10", "internet explorer"});
  browsers.add(new String[]{Platform.MAC.toString(), "5", "safari"});
 browsers.add(new String[]{Platform.MAC.toString(), "7", "iphone"});
browsers.add(new String[]{Platform.LINUX.toString(), "30", "chrome"});
 browsers.add(new String[]{Platform.ANDROID.toString(), "4.0", "android"});
return browsers;
}
private WebDriver driver;
private String sessionId;

@Before
public void setUp() throws Exception {

DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.BROWSER_NAME, browser);
capabilities.setCapability(CapabilityType.VERSION, version);
capabilities.setCapability(CapabilityType.PLATFORM,  Platform.valueOf(os));

if(browser.equalsIgnoreCase("android"))
{
 capabilities.setCapability("device-type", "tablet");
  capabilities.setCapability("device-orientation", "portrait");
}
else if(browser.equalsIgnoreCase("iphone"))
{
  capabilities.setCapability("device-orientation", "portrait");
}

String name = testName.getMethodName() + ": "+ browser+ " "+ version+ " "+ Platform.valueOf(os);
capabilities.setCapability("name",name);
this.driver= new RemoteWebDriver(new URL("http://"+ authentication.getUsername() + ":"+ authentication.getAccessKey() + "@ondemand.saucelabs.com:80/wd/hub"),capabilities);
this.sessionId= ((RemoteWebDriver)driver).getSessionId().toString();

}

@Override
public String getSessionId() {
return sessionId;
}

@Test
public void webDriver() throws Exception {
// Simple test -just go to the site and verify the title.
 driver.get("https://www.google.com/");
 assertEquals("To Do List", driver.getTitle());
}

@After
public void tearDown() throws Exception {
 driver.quit(); }   }

这就是抛出

java.lang.Exception: Test class should have exactly one public constructor
    at org.junit.runners.BlockJUnit4ClassRunner.validateOnlyOneConstructor(BlockJUnit4ClassRunner.java:158)
    at org.junit.runners.parameterized.BlockJUnit4ClassRunnerWithParameters.validateConstructor(BlockJUnit4ClassRunnerWithParameters.java:88)
    at org.junit.runners.BlockJUnit4ClassRunner.collectInitializationErrors(BlockJUnit4ClassRunner.java:127)
    at org.junit.runners.ParentRunner.validate(ParentRunner.java:416)
    at org.junit.runners.ParentRunner.<init>(ParentRunner.java:84)
    at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:65)
    at org.junit.runners.parameterized.BlockJUnit4ClassRunnerWithParameters.<init>(BlockJUnit4ClassRunnerWithParameters.java:27)
    at org.junit.runners.parameterized.BlockJUnit4ClassRunnerWithParametersFactory.createRunnerForTestWithParameters(BlockJUnit4ClassRunnerWithParametersFactory.java:16)
    at org.junit.runners.Parameterized.createRunnersForParameters(Parameterized.java:313)
    at org.junit.runners.Parameterized.<init>(Parameterized.java:248)
    at com.saucelabs.junit.Parallelized.<init>(Parallelized.java:88)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
    at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)
    at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.<init>(JUnit4TestReference.java:33)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.<init>(JUnit4TestClassReference.java:25)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:48)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

exception while running test. Can someone help finding the solution.
sauutmhj

sauutmhj1#

private WebDriverTest(String os, String version, String browser) {
 super();
 this.os= os;
 this.version= version;
 this.browser= browser;
}

应该是

public WebDriverTest(String os, String version, String browser) {
 super();
 this.os= os;
 this.version= version;
 this.browser= browser;
}
1l5u6lss

1l5u6lss2#

在编写JUnit时要记住的是,在执行时,它在执行测试方法之前调用测试类的构造函数。您可以通过在构造函数本身中放置一条System.out.println消息来验证它
在这里,您已经在代码中将构造函数定义为私有,因此测试框架将无法示例化测试类以执行测试方法,将作用域更改为公共。

public WebDriverTest(String os, String version, String browser) {
 super();
 this.os= os;
 this.version= version;
 this.browser= browser;
}
2skhul33

2skhul333#

当我试图在Eclipse中运行单元测试时,我得到了这个错误,结果发现问题出在Eclipse上。使用Gradle从命令行运行它会返回一个不同的错误--另一个单元测试无效。修复其他无效测试修复了此错误。太感谢你了,月食!

igetnqfo

igetnqfo4#

测试类必须是公共的,或者如果它有构造函数,则必须是公共的才能使连接工作。

相关问题