Java 通过TestNg运行代码时出错
运行以下代码时出现配置错误。因此,我无法采取进一步行动。有人能帮我运行脚本吗 ==============================================================================================Java 通过TestNg运行代码时出错,java,selenium,Java,Selenium,运行以下代码时出现配置错误。因此,我无法采取进一步行动。有人能帮我运行脚本吗 ============================================================================================== 包装Flipkart import org.testng.annotations.Test; import org.testng.annotations.BeforeMethod; import java.
包装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();
}
在before方法中,在此行中有局部变量
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);
}
@before方法
方法()之前的公共无效{
System.setProperty(“webdriver.chrome.driver”,“C:\\chrome\\chromedriver.exe”);
ChromeOptions选项=新的ChromeOptions();
Map prefs=新的HashMap();
prefs.put(“凭证启用服务”,false);
prefs.put(“密码管理器已启用”,false);
选项。设置实验选项(“prefs”,prefs);
选项。添加参数(“开始最大化”);
options.addArguments(“禁用信息栏”);
驱动程序=新的色度驱动程序(可选);
}
您犯了一个错误
WebDriver是一个类变量,即该类的任何方法都可以使用它
在@BeforeMethod中,您写了一行:
WebDriver=新的ChromeDriver(选项)。请换成
驱动程序=新的色度驱动程序(可选)强>
您正在创建一个Webdriver类型的单独对象,类变量driver的值仍然为“null”,因此您得到的是null指针异常。您的答案与@vijayjawale Answer有什么不同?谢谢,你说得对。答案也一样。当我开始写答案时,它不在那里。
@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);
}