我正在尝试针对Internet Explorer运行TestNG Suite。但是,似乎没有尝试使用CSS定位器在网页上找到元素。我使用@FindAll注释来指定多个定位策略,这些策略适用于Chrome和Firefox。以下是我在TestNG失败摘要中看到的错误:
org.openqa.selenium.WebDriverException :(警告:服务器没有 提供任何堆栈跟踪信息)命令持续时间或超时:70 毫秒构建信息:版本:' 2.53.0',修订版:' 35ae25b',时间: ' 2016-03-15 17:00:58'系统信息:主机:'(主机名在此省略)',ip: ' 10.18.28.53',os.name:' Windows 10',os.arch:' amd64',os.version: ' 10.0',java.version:' 1.8.0_91'司机信息: org.openqa.selenium.ie.InternetExplorerDriver功能 [{browserAttachTimeout = 0,ie.enableFullPageScreenshot = true, enablePersistentHover = true,ie.forceCreateProcessApi = false, ie.forceShellWindowsApi = false,pageLoadStrategy = normal, ignoreZoomSetting = false,ie.fileUploadDialogTimeout = 3000,version = 11, platform = WINDOWS,nativeEvents = true,ie.ensureCleanSession = false, elementScrollBehavior = 0,ie.browserCommandLineSwitches =, requireWindowFocus = false,browserName = internet explorer, initialBrowserUrl = http://localhost:28843/,javascriptEnabled = true, ignoreProtectedModeSettings = false,enableElementCacheCleanup = true, unexpectedAlertBehaviour = dismiss}]会话ID: 3ccb2fc8-aac7-484c-8a0d-ca8c8539742e ***元素信息:{使用= css选择器,值=#content> div.container> h1} at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(未知 来源)at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(未知 来自java.lang.reflect.Constructor.newInstance(未知来源) 在 org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206) 在 org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158) 在 org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:678) 在 org.openqa.selenium.remote.RemoteWebDriver.findElements(RemoteWebDriver.java:388) 在 org.openqa.selenium.remote.RemoteWebDriver.findElementsByCssSelector(RemoteWebDriver.java:496) 在org.openqa.selenium.By $ ByCssSelector.findElements(By.java:441)at at org.openqa.selenium.support.pagefactory.ByAll.findElements(ByAll.java:63) 在 org.openqa.selenium.support.pagefactory.ByAll.findElement(ByAll.java:52) 在 org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:355) 在 org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69) 在 org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:38) 在com.sun.proxy。$ Proxy13.getText(未知来源)at com.webcp.common.pageobjects.LoginPage.getLoginTextTop(LoginPage.java:58) 在 com.webcp.common.pageobjects.LoginPage.verifyLoginTextTop(LoginPage.java:65) 在 com.webcp.common.tests.LoginPageTest.verifyLoginPage(LoginPageTest.java:45) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(未知来源)at sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源)at java.lang.reflect.Method.invoke(未知来源)at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85) 在org.testng.internal.Invoker.invokeMethod(Invoker.java:639)at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:816)at at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1124)at at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125) 在 org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108) 在org.testng.TestRunner.privateRun(TestRunner.java:774)at org.testng.TestRunner.run(TestRunner.java:624)at org.testng.SuiteRunner.runTest(SuiteRunner.java:359)at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:354)at at org.testng.SuiteRunner.privateRun(SuiteRunner.java:312)at at org.testng.SuiteRunner.run(SuiteRunner.java:261)at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)at org.testng.TestNG.runSuitesSequentially(TestNG.java:1215)at at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)at at org.testng.TestNG.run(TestNG.java:1048)at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:126) 在org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:137) 在org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:58)
以下是正在运行的代码块:
TestBaseSetup类片段:
public class TestBaseSetup
{
private static WebDriver driver;
private static Logger Log = Logger.getLogger(TestBaseSetup.class.getName());
static String driverPath = "C:\\Users\\bberndt\\eclipse\\Selenium Files\\";
public static WebDriver getDriver()
{
return driver;
}
private static WebDriver initIEDriver(String appURL)
{
Log.info("Launching Microsoft Internet Explorer with new profile");
File file = new File(driverPath + "IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
WebDriver driver = new InternetExplorerDriver();
Log.info("Driver instantiated");
driver.manage().window().maximize();
Log.info("Window maximized");
driver.get(appURL);
Log.info("Navigating to: " + appURL);
return driver;
}
}
LoginPageTest类:
public class LoginPageTest extends TestBaseSetup
{
private WebDriver driver;
private LoginPage loginPage;
private HomePage homePage;
private SoftAssert softAssert;
private static Logger Log = Logger.getLogger(LoginPageTest.class.getName());
private String sTestCaseName;
private int iTestCaseRow;
@BeforeMethod
public void setUpTest()
{
driver = getDriver();
Log.info("Driver loaded");
//loginPage = new LoginPage(driver);
loginPage = PageFactory.initElements(driver, LoginPage.class);
homePage = PageFactory.initElements(driver, HomePage.class);
softAssert = new SoftAssert();
}
@Test(priority = 1)
public void verifyLoginPage()
{
Log.info("--------------------------START TEST---------------------------");
Log.info("Test 1: verifyLoginPage");
softAssert.assertTrue(loginPage.verifyLoginPageTitle(), "Login page title does not match\n");
softAssert.assertTrue(loginPage.verifyLoginTextTop(), "Login text top does not match\n");
softAssert.assertTrue(loginPage.verifyLoginTextBottom(), "Login text bottom does not match\n");
softAssert.assertAll();
}
}
LoginPage对象存储库:
public class LoginPage
{
protected WebDriver driver;
private static Logger Log = Logger.getLogger(LoginPage.class.getName());
@FindAll({ @FindBy(xpath = "/html/body/div/div/div/h1"),
@FindBy(css = "#content > div.container > h1") })
private WebElement loginTextTop;
@FindAll({ @FindBy(xpath = "/html/body/div/div/div/h2"),
@FindBy(css = "h2") })
private WebElement loginTextBottom;
public LoginPage(WebDriver driver)
{
this.driver = driver;
}
public String getPageTitle()
{
String title = driver.getTitle();
return title;
}
public boolean verifyLoginPageTitle()
{
String expectedPageTitle = "WTS";
return getPageTitle().contains(expectedPageTitle);
}
public String getLoginTextTop()
{
String TextTop = loginTextTop.getText();
return TextTop;
}
public boolean verifyLoginTextTop()
{
String expectedPageText = "Welcome to CenterPoint™";
return getLoginTextTop().contains(expectedPageText);
}
public String getLoginTextBottom()
{
String TextBottom = loginTextBottom.getText();
return TextBottom;
}
public boolean verifyLoginTextBottom()
{
String expectedPageText = "Please Sign In";
return getLoginTextBottom().contains(expectedPageText);
}
}
答案 0 :(得分:0)
原来这不是代码的问题。相反,Internet选项中有一个设置需要打开才能使其正常工作。 高级>设置>安全>允许活动内容在“我的电脑”上的文件中运行。
我选中此框后,我的IE测试按预期工作。