很抱歉,如果以前已经回答了这个问题,但是我已经看了一下,却找不到任何可以帮助我的东西。尝试运行以下代码时,标题出现错误。有人知道可以使用哪种显式构造函数来解决此错误吗?
public class HomePageTest extends TestBase{
LoginPage loginPage;
HomePage homePage;
ShopByDepartmentPage shopByDepartmentPage;
JamiesAmazonPage jamiesAmazonPage;
TodaysDealsPage todaysDealsPage;
MyAccountPage myAccountPage;
BasketPage basketPage;
public HomePageTest() throws IOException {
super();
}
@BeforeMethod
public void setUp() throws IOException {
initialization();
loginPage = new LoginPage();
shopByDepartmentPage = new ShopByDepartmentPage();
jamiesAmazonPage = new JamiesAmazonPage();
todaysDealsPage = new TodaysDealsPage();
myAccountPage = new MyAccountPage();
basketPage = new BasketPage();
homePage = loginPage.login(prop.getProperty("username"),
prop.getProperty("password"));
}
}
我的测试基础代码是:
public static WebDriver driver;
public static Properties prop;
public TestBase() throws IOException {
try {
prop = new Properties();
FileInputStream fis = new FileInputStream("D:\\Users\\mcalpinej\\Desktop\\POM Project\\POM_Project\\src\\main\\java\\Config\\config.properties");
prop.load(fis);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void initialization() {
if (prop.getProperty("browser").equals("firefox"))
{
System.setProperty("webdriver.gecko.driver", "D:\\Users\\mcalpinej\\Desktop\\POM Project\\POM_Project\\src\\main\\java\\Resources\\geckodriver.exe");
driver = new FirefoxDriver();
}
else if (prop.getProperty("browser").equals("chrome"))
{
System.setProperty("webdriver.chrome.driver", "D:\\Users\\mcalpinej\\Desktop\\POM Project\\POM_Project\\src\\main\\java\\Resources\\chromedriver.exe");
driver = new ChromeDriver();
}
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().pageLoadTimeout(TestUtil.PAGE_LOAD_TIMEOUT, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(TestUtil.IMPLICIT_WAIT, TimeUnit.SECONDS);
driver.get(prop.getProperty("url"));
}
全栈跟踪代码如下:
FAILED CONFIGURATION: @BeforeMethod setUp
java.lang.Error: Unresolved compilation problem:
Default constructor cannot handle exception type IOException thrown by
implicit super constructor. Must define an explicit constructor
at Pages.ShopByDepartmentPage.<init>(ShopByDepartmentPage.java:5)
at TestCases.HomePageTest.setUp(HomePageTest.java:38)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
at org.testng.internal.MethodInvocationHelper.invokeMethodConsideringTimeout(MethodInvocationHelper.java:59)
at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:458)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:222)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:523)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:648)
at org.testng.TestRunner.run(TestRunner.java:505)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
at org.testng.SuiteRunner.run(SuiteRunner.java:364)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
at org.testng.TestNG.runSuites(TestNG.java:1049)
at org.testng.TestNG.run(TestNG.java:1017)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
当我尝试将@Before方法放入尝试捕获中时,如下所示,我仍然收到相同的错误消息:
@BeforeMethod
public void setUp() {
initialization();
try {
loginPage = new LoginPage();
shopByDepartmentPage = new ShopByDepartmentPage();
jamiesAmazonPage = new JamiesAmazonPage();
todaysDealsPage = new TodaysDealsPage();
myAccountPage = new MyAccountPage();
basketPage = new BasketPage();
homePage = loginPage.login(prop.getProperty("username"), prop.getProperty("password"));
}
catch (Exception e) {
e.printStackTrace();
}
ShopByDepartment页面代码:
public class ShopByDepartmentPage extends TestBase{
@FindBy(xpath="//tr//td[1]//div[1]//ul[1]//li[1]")
WebElement videos;
@FindBy(xpath="//li[@class='nav_first']//a[@class='nav_a'][contains(text(),'Books')]")
WebElement books;
@FindBy(xpath="//li[@class='nav_first']//a[@class='nav_a'][contains(text(),'Amazon Music Unlimited')]")
WebElement music;
@FindBy(xpath="//tr//td[2]//div[4]//ul[1]//li[6]//a[1]")
WebElement computerGames;
答案 0 :(得分:0)
听起来您的单元测试框架无法处理从构造方法中抛出的选中Exception
。
最明显的建议是-不要扔Exception
。如果框架不允许,则可能是有充分的理由-即,它可能不会以很好的方式显示Exception
。
如果您确实确实想在构造函数中执行此逻辑,则另一种方法是将IOException
作为ExceptionInInitializerError
重新抛出,这是未经检查的Exception
。
答案 1 :(得分:0)
报告的即时错误是Unresolved compilation problem
的事实暗示了其他情况:这种类型的错误可能由使用eclipse编译器编译的字节码产生,该编译器执行增量编译。
似乎在某些时候您的派生类中没有显式的默认构造函数,并且您的IDE(可能是eclipse,但可能是另一个使用eclipse编译器的)和/或构建系统无法从那时开始重新编译该类,因此您仍在运行旧的字节码。
尝试进行干净的构建,并注意其他编译错误。如果您拥有诸如maven或gradle之类的适当构建系统,请关闭您的IDE并使用该系统运行clean&build。构建系统可能会拾取IDE即时生成的.class
文件。