我跟随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.
答案 0 :(得分:4)
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;
}
答案 1 :(得分:0)
在编写JUnit时指向记忆是在执行时它在执行测试方法之前调用测试类的构造函数。您可以通过在构造函数本身中放置System.out.println消息来验证它
在您的代码中,您已将构造函数定义为私有,因此测试框架将无法为测试方法实例化测试类执行将范围更改为public。
public WebDriverTest(String os, String version, String browser) {
super();
this.os= os;
this.version= version;
this.browser= browser;
}
答案 2 :(得分:0)
在尝试在Eclipse中运行单元测试时遇到此错误,结果证明问题出在Eclipse。使用gradle从命令行运行它返回了另一个错误-另一个单元测试无效。修复了其他无效的测试,从而修复了此错误。非常感谢,Eclipse!