在使用Cucumber框架执行脚本时,我经常遇到此错误。 使用给定的URL成功启动了浏览器,并在登录后也验证了元素的存在。但是,当我尝试单击该元素时,它失败并给出以下错误。
我也很疲倦地使用以下方法执行代码: 1.使用TestNG 2.作为Java应用程序
public class POMTestRunner {
public static WebDriver driver;
public static WebDriverWait wait;
@BeforeSuite
public void initializeTest() {
System.setProperty("webdriver.chrome.driver", UtilityClass.ChromeDriverpath);
ChromeOptions Options = new ChromeOptions();
Options.addArguments("start-maximized");
Options.addArguments("disable-notifications");
Options.setPageLoadStrategy(PageLoadStrategy.NORMAL);
driver = new ChromeDriver(Options);
wait = new WebDriverWait(driver,60);
driver.get(UtilityClass.AppURL);
}
@Test
public void RequirementOne() throws Exception {
PageMethods ReqOne = new PageMethods(driver,wait);
ReqOne.SalesForceLogin();
ReqOne.waitForJQueryToBeActive();
ReqOne.NewOpportunityCreation();
ReqOne.ValidateDealSize();
}
@AfterSuite
public void endTest() {
driver.quit();
driver=null;
}
PageMethodClass:
public class PageMethods extends PageObjectsClass {
public WebDriver driver;
public WebDriverWait wait;
public PageMethods(WebDriver driver, WebDriverWait wait) {
this.driver=driver;
this.wait=wait;
}
public void waitForJQueryToBeActive() {
Boolean isJqueryUsed = (Boolean) ((JavascriptExecutor) driver)
.executeScript("return (typeof(jQuery) != 'undefined')");
if (isJqueryUsed) {
while (true) {
// JavaScript test to verify jQuery is active or not
Boolean ajaxIsComplete = (Boolean) (((JavascriptExecutor) driver)
.executeScript("return jQuery.active == 0"));
if (ajaxIsComplete)
break;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
}
}
}
}
public void SalesForceLogin() {
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("username"))).sendKeys(UtilityClass.Username);
driver.findElement(By.id("password")).sendKeys(UtilityClass.Password);
driver.findElement(By.id("Login")).click();
String OpportunityTab = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[text()='Opportunities']"))).getText();
if(OpportunityTab.contains("Opportunities")) {
System.out.println("Login Successful");
}else {
System.out.println("Login Failed. Abort Execution");
}
}
public void NewOpportunityCreation() throws InterruptedException {
Thread.sleep(5000);
wait.until(ExpectedConditions.visibilityOfElementLocated(OpportunitiesTab)).click();
wait.until(ExpectedConditions.visibilityOfElementLocated(NewOpportunityButton)).click();
wait.until(ExpectedConditions.visibilityOfElementLocated(OpportunityName)).sendKeys("AutomationFrameworkOpportunity");
driver.findElement(OpportunityAmount).sendKeys(UtilityClass.Amount);
driver.findElement(OpportunityCloseDateCalendar).click();
wait.until(ExpectedConditions.visibilityOfElementLocated(OpportunityCloseDate)).click();
driver.findElement(Stage).click();
driver.findElement(OpportunityStageQualification).click();
driver.findElement(SaveButton).click();
}
public void ValidateDealSize() {
wait.until(ExpectedConditions.visibilityOfElementLocated(OpportunityDetailsTab)).click();
String DealSize = wait.until(ExpectedConditions.visibilityOfElementLocated(DealSizeValue)).getText();
Integer AmountValue = Integer.parseInt(UtilityClass.Amount);
if(AmountValue<1000) {
Assert.assertEquals(DealSize, "");
}
else if(AmountValue>50000) {
Assert.assertEquals(DealSize, "Big Deal");
}
else if(AmountValue>1000 && AmountValue<=9999) {
Assert.assertEquals(DealSize, "Small");
}
else if(AmountValue>10000 && AmountValue<=50000) {
Assert.assertEquals(DealSize, "Medium");
}
else {
System.out.println("Entered Opportunity Amount Does not match any criteria. Deal Size not validated.");
}
}
}
试图通过论坛上的现有问题找到答案,但找不到该问题的解决方案。 还想知道该错误的确切含义。
错误堆栈跟踪:
Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: Cannot read property 'defaultView' of undefined
(Session info: chrome=73.0.3683.103)
(Driver info: chromedriver=72.0.3626.69 (3c16f8a135abc0d4da2dff33804db79b849a7c38),platform=Windows NT 10.0.17763 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:25:48'
System info: host: 'PDC2LAP-7173253', ip: '10.170.9.243', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_201'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, acceptSslCerts: false, applicationCacheEnabled: false, browserConnectionEnabled: false, browserName: chrome, chrome: {chromedriverVersion: 72.0.3626.69 (3c16f8a135abc..., userDataDir: C:\Users\KOVIDA~1.MEH\AppDa...}, cssSelectorsEnabled: true, databaseEnabled: false, goog:chromeOptions: {debuggerAddress: localhost:60915}, handlesAlerts: true, hasTouchScreen: false, javascriptEnabled: true, locationContextEnabled: true, mobileEmulationEnabled: false, nativeEvents: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, proxy: Proxy(), rotatable: false, setWindowRect: true, takesHeapSnapshot: true, takesScreenshot: true, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unexpectedAlertBehaviour: ignore, unhandledPromptBehavior: ignore, version: 73.0.3683.103, webStorageEnabled: true}
Session ID: 957f6f09e6c094d82a7addda8a37ee66
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:423)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:214)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:166)
at org.openqa.selenium.remote.http.JsonHttpResponseCodec.reconstructValue(JsonHttpResponseCodec.java:40)
at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:80)
at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:44)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:285)
at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:84)
at stepDefinitionTOD.PageMethods.NewOpportunityCreation(PageMethods.java:36)
at testrunnerTOD.POMTestRunner.main(POMTestRunner.java:53)
我使用了Explicit wait和Thread.Sleep来确保DOM在交互之前完全加载。页面加载策略也已被使用。
使用以下先前回答的问题中提到的方法: 1.为页面加载策略添加了chromedriverOptions: Options.setPageLoadStrategy(PageLoadStrategy.NORMAL); 2.在出现错误的方法之前添加了WaitForAjax2Complete。 3.使用WebDriverWait定位并单击元素。
如果还有其他方法,请回答。