我收到NullPointerException。 PFA代码。我是初学者,在Selenium中使用TestNG。
有两个软件包。 基本类在package0中,其余的其他类在package1中。
我要做什么?
- 我创建了一个Source类并编写了自动建议框
代码。
- 在建议中创建了Source类的对象
类。
-
将Source类中的方法调用到 建议类给出了一个NullPointerException错误。
但是 当我使用TestNG将Source类作为独立类运行时- 工作正常。package package1; import java.io.IOException; import java.util.List; import org.openqa.selenium.*; import org.openqa.selenium.support.ui.*; import package0.Baseclass; public class Source extends Baseclass{ public void autoSuggestion() throws Exception { String autoSuggetionBoxOptionSourceSelection; driver.get(getDataFromPropFile("url")); WebElement orgin = driver.findElement(By.xpath(getDataFromPropFile("origin"))); orgin.sendKeys(getDataFromPropFile("originvalue")); WebElement autoSuggestionSourceBox= driver.findElement(By.xpath(getDataFromPropFile("autosuggestionsourcebox"))); WebDriverWait wait = new WebDriverWait(driver,10); wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(("(//*[@class='suggestion-box__list'])[1]")))); List<WebElement> autoSuggestionBoxSourceoptions = autoSuggestionSourceBox.findElements(By.tagName("li")); //System.out.println("Number of options are:" + autoSuggestionBoxSourceoptions.size()); for (int i=0; i<autoSuggestionBoxSourceoptions.size(); i++) { autoSuggetionBoxOptionSourceSelection = autoSuggestionBoxSourceoptions.get(i).getText(); System.out.println(autoSuggetionBoxOptionSourceSelection); if (autoSuggetionBoxOptionSourceSelection.contains("LSH")) { autoSuggestionBoxSourceoptions.get(i).click(); break; } } } } package package1; import java.io.IOException; import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.WebElement; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import org.testng.annotations.Test; import package0.Baseclass; import package1.Source; public class Suggestions extends Baseclass { @Test public void auto() throws Exception { String autoSuggetionBoxOptionDestinationSelection; Source src = new Source(); src.autoSuggestion(); // WebElement point = driver.switchTo().activeElement(); // // point.sendKeys("LAS"); // WebDriverWait wait1 = new WebDriverWait(driver,10); // wait1.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("(//*[@class='suggestion-box__list'])[2]"))); // WebElement autoSuggestionDestinationBox= driver.findElement(By.xpath("(//*[@class='suggestion-box__list'])[2]")); // List<WebElement> autoSuggestionBoxDestinationoptions = autoSuggestionDestinationBox.findElements(By.tagName("li")); // // for(int i=0;i<autoSuggestionBoxDestinationoptions.size();i++) // { // autoSuggetionBoxOptionDestinationSelection = autoSuggestionBoxDestinationoptions.get(i).getText(); // if (autoSuggetionBoxOptionDestinationSelection.contains("VGT")) // { // autoSuggestionBoxDestinationoptions.get(i).click(); // break; // } // } // // driver.switchTo().activeElement(); // String startXP= "(//*[@class='calendar__single-month active'])[1]/div[3]/div["; // int i=1; // String endXP="]/div"; // String path= startXP+i+endXP; // // for (i =1;i<=42;i++) // { // try { // path = startXP+i+endXP; // String date = driver.findElement(By.xpath(path)).getText(); // System.out.println("Date Is:" + date); // // if (date.equals("15")) // { // driver.findElement(By.xpath(path)).click(); // break; // } // } // catch (Exception e) { // // System.out.println("No date present for xpath = " + path); // System.out.println("Exception = " + e); // // } // } } } package package0; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Properties; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.ie.InternetExplorerDriver; import org.testng.annotations.*; public class Baseclass { public WebDriver driver; public Properties prop = new Properties(); @BeforeMethod public void browserSetup() { String path = System.getProperty("user.dir"); System.setProperty("webdriver.chrome.driver",path+"\\browserDrivers\\chromedriver.exe"); //System.setProperty("webdriver.gecko.driver",path+"\\browserDrivers\\geckodriver.exe"); driver = new ChromeDriver(); //driver = new FirefoxDriver(); //driver.manage().window().maximize(); //System.setProperty("webdriver.ie.driver",path + "\\browserDrivers\\IEDriverServer.exe"); //driver = new InternetExplorerDriver(); driver.manage().window().maximize(); } public String getDataFromPropFile(String key) throws IOException { String projectPath = System.getProperty("user.dir"); String propFilePath = projectPath+"\\data.properties"; File pathFile = new File(propFilePath); FileInputStream fIP= new FileInputStream(pathFile); prop.load(fIP); return prop.getProperty(key); } @AfterMethod public void closeBrowser() { driver.close(); } }