運行下面的代碼時出現配置錯誤。因此,我無法做進一步的行動。有人可以幫助我運行腳本。通過TestNg運行代碼時出錯
============================================== ==========================
package 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("[email protected].com");
driver.findElement(By.id("j_password")).sendKeys("[email protected]");
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 [email protected]: 81 ms
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 54 ms
[TestNG] Time taken by [email protected]: 70 ms
[TestNG] Time taken by [email protected]: 149 ms
[TestNG] Time taken by [email protected]: 7 ms
[TestNG] Time taken by [email protected]: 5 ms
你的回答與@vijayjawale不同嗎?謝謝 – DebanjanB
是的,你是對的。答案沒有什麼不同。當我開始寫答案時,它不在那裏。 – Monika