运行以下代码时出现配置错误。因此,我无法采取进一步行动。有人可以帮我运行脚本。
=============================================== =========================
包Flipkart;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import java.util.HashMap;
import java.util.Map;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.annotations.AfterMethod;
public class report {
WebDriver driver;
@Test
public void f() throws InterruptedException {
driver.get("https://www.coopathome.ch/de/");
Thread.sleep(2000);
driver.findElement(By.xpath(".//*[@id='wa-badge']/span")).click();
driver.findElement(By.xpath(".//*[@id='mastheadMyAccountLink']/span")).click();
//driver.manage().timeouts().implicitlyWait(2000, TimeUnit.MILLISECONDS);
Thread.sleep(2000);
driver.findElement(By.id("j_username")).sendKeys("tinamalhotra325@gmail.com");
driver.findElement(By.id("j_password")).sendKeys("google@123");
driver.findElement(By.id("loginOverlaySubmitLoginFormButton")).click();
Thread.sleep(2000);
driver.findElement(By.xpath(".//*[@id='nav']/ul/li[7]/a/span")).click();
Thread.sleep(2000);
driver.findElement(By.xpath(".//*[@id='subnav--item-m_0222']/li[1]/ul[1]/li[2]/a")).click();
Thread.sleep(2000);
driver.findElement(By.xpath("html/body/div[3]/div/div[2]/div[2]/div/div[4]/ul/li[8]/div/div/div[3]/button")).click();
Thread.sleep(2000);
driver.findElement(By.xpath("html/body/div[3]/div/div[2]/div[2]/div/div[4]/ul/li[8]/div/div/div[3]/div/div/button[2]")).click();
driver.findElement(By.xpath(".//*[@id='nav']/ul/li[7]/a/span")).click();
Thread.sleep(2000);
driver.findElement(By.xpath(".//*[@id='subnav--item-m_0222']/li[1]/ul[1]/li[1]/a")).click();
driver.findElement(By.xpath("html/body/div[3]/div/div[2]/div[2]/div/div[4]/ul/li[3]/div/div/div[4]/button")).click();
Thread.sleep(2000);
driver.findElement(By.xpath("html/body/div[3]/div/div[2]/div[2]/div/div[4]/ul/li[3]/div/div/div[4]/div/div/button[2]")).click();
driver.findElement(By.xpath(".//*[@id='nav']/ul/li[1]/a/span")).click();
Thread.sleep(2000);
driver.findElement(By.xpath(".//*[@id='subnav--item-m_0001']/li[1]/ul[1]/li[1]/a")).click();
Thread.sleep(2000);
driver.findElement(By.xpath("html/body/div[3]/div/div[2]/div[2]/div/div[4]/ul/li[1]/div/div/div[3]/button")).click();
// WebElement apple_price=driver.findElement(By.xpath("html/body/div[3]/div/div[2]/div[2]/div/div[4]/ul/li[1]/div/div/div[3]/button"));
// if (apple_price.getText().equals("3.80"))
// {
// System.out.println("price matched");
//
// }
// else {
// System.out.println("title not matched expected title is "+ apple_price.getText());
// }
Thread.sleep(2000);
driver.findElement(By.xpath("html/body/div[3]/div/div[2]/div[2]/div/div[4]/ul/li[1]/div/div/div[3]/div/div/button[2]")).click();
driver.findElement(By.xpath(".//*[@id='nav']/ul/li[2]/a/span")).click();
Thread.sleep(2000);
driver.findElement(By.xpath(".//*[@id='subnav--item-m_0055']/li[1]/ul[1]/li[1]/a")).click();
driver.findElement(By.xpath("html/body/div[3]/div/div[2]/div[2]/div/div[4]/ul/li[5]/div/div/div[3]/button")).click();
driver.findElement(By.xpath("html/body/div[3]/div/div[2]/div[2]/div/div[4]/ul/li[2]/div/div/div[3]/button")).click();
Thread.sleep(2000);
driver.findElement(By.id("miniCartProceedToCheckoutButton")).click();
Thread.sleep(2000);
driver.findElement(By.id("miniCartProceedToCheckoutButton")).click();
Thread.sleep(2000);
driver.findElement(By.xpath(".//*[@id='heimlieferung']/div[4]/dl/dd[2]/a")).click();
driver.findElement(By.id("chooseDeliveryDate_continue_button")).click();
WebElement t=driver.findElement(By.xpath("html/body/div[3]/div/div[3]/div/dd"));
if (t.getText().equals("120.40"))
{
System.out.println("price matched");
}
else {
System.out.println("title not matched expected title is "+ t.getText());
}
// String Total_Cart=driver.findElement(By.xpath("html/body/div[3]/div/div[3]/div/dd")).getText();
// System.out.println(Total_Cart);
// if
// Assert.assertEquals(Total_Cart, 120);
// System.out.println("Test case passed");
}
@BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver","C:\\Chrome\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("credentials_enable_service", false);
prefs.put("password_manager_enabled", false);
options.setExperimentalOption("prefs", prefs);
options.addArguments("start-maximized");
options.addArguments("disable-infobars");
WebDriver driver = new ChromeDriver(options);
}
@AfterMethod
public void afterMethod() {
driver.close();
}
Error:
[TestNG] Running:
C:\Users\SalunkheP\AppData\Local\Temp\testng-eclipse-1281031724\testng-customsuite.xml
Starting ChromeDriver 2.25.426923 (0390b88869384d6eb0d5d09729679f934aab9eed) on port 18277
Only local connections are allowed.
FAILED CONFIGURATION: @AfterMethod afterMethod
java.lang.NullPointerException
at Flipkart.report.afterMethod(report.java:105)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)
at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:510)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:211)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:703)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:816)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1124)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
at 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 org.testng.SuiteRunner.privateRun(SuiteRunner.java:312)
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 org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
at org.testng.TestNG.run(TestNG.java:1048)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)
FAILED: f
java.lang.NullPointerException
at Flipkart.report.f(report.java:21)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:639)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:816)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1124)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
at 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 org.testng.SuiteRunner.privateRun(SuiteRunner.java:312)
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 org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
at org.testng.TestNG.run(TestNG.java:1048)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)
===============================================
Default test
Tests run: 1, Failures: 1, Skips: 0
Configuration Failures: 1, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 1, Failures: 1, Skips: 0
Configuration Failures: 1, Skips: 0
===============================================
[TestNG] Time taken by org.testng.reporters.XMLReporter@649d209a: 81 ms
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 54 ms
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@6fffcba5: 70 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@3ab39c39: 149 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter2@16c0663d: 7 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@ea4a92b: 5 ms
答案 0 :(得分:1)
在您的beforeMethod中,您在此行中有本地变量driver
:
WebDriver driver = new ChromeDriver(options);
必须如此:
driver = new ChromeDriver(options);
方法如下:
@BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver","C:\\Chrome\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("credentials_enable_service", false);
prefs.put("password_manager_enabled", false);
options.setExperimentalOption("prefs", prefs);
options.addArguments("start-maximized");
options.addArguments("disable-infobars");
driver = new ChromeDriver(options);
}
答案 1 :(得分:0)
你犯了一个错误。
WebDriver驱动程序; 是一个类变量,即类的任何方法都可以使用它。
在@BeforeMethod中,你写了一句话: WebDriver驱动程序=新的ChromeDriver(选项); 。请更换 driver = new ChromeDriver(选项);
您正在创建一个Webdriver类型的单独对象,类变量驱动程序的值仍为“null”,因此您将获得空指针异常。