异常捕获的是:java.lang.NullPointerException FAILED:在PriceLine.com.ApplicationUtilities.returnWebElement(ApplicationUtilities.java:40)上的SearchFlightAndCars java.lang.NullPointerException,在priceLine.com.SeleniumTest.SearchFlightAndCars上的priceLine.com.MainSetUp.Login(MainSetUp.java:45)
` ApplicationUtitlies.java
package priceLine.com;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class ApplicationUtilities
{
WebElement myElement;
WebDriver driver;
public ApplicationUtilities(WebDriver driver) {
this.driver = driver;
}
public WebElement returnWebElement(String propType,String propValue)
{
try
{
if(propType.equals("id"))
myElement = driver.findElement(By.id(propValue));
else if(propType.equals("name"))
myElement = driver.findElement(By.name(propValue));
else if(propType.equals("className"))
myElement = driver.findElement(By.className(propValue));
else if(propType.equals("xpath"))
myElement = driver.findElement(By.xpath(propValue));
else if(propType.equals("cssSelector"))
myElement = driver.findElement(By.cssSelector(propValue));
else if(propType.equals("linkText"))
myElement = driver.findElement(By.linkText(propValue));
else if(propType.equals("partialLinkText"))
myElement = driver.findElement(By.partialLinkText(propValue));
else if(propType.equals("tagName"))
myElement = driver.findElement(By.tagName(propValue));
}
catch(Exception e)
{
System.out.println("exception caught is :"+e);
}
if(myElement.isEnabled())
return myElement;
else
System.out.println("element cannot be found");
return null;
}
}`
` ReadProperties.java
package priceLine.com;
import java.io.FileInputStream;
import java.util.Properties;
public class ReadProperties {
static Properties prop;
static FileInputStream file;
public static String readValue(String key)
{
try {
file = new FileInputStream("F:\\SeleniumLearning\\SeleniumPractice\\TestData\\Data.properties");
prop = new Properties();
prop.load(file);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return prop.getProperty(key);
}
}`
` MainSetUp.java
package priceLine.com;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import priceLine.com.ReadProperties;
public class MainSetUp {
WebElement myElement;
public WebDriver driver;
ApplicationUtilities myUtil = new ApplicationUtilities(driver);
public WebDriver setUp()
{
String browserName = ReadProperties.readValue("browser");
if(browserName.equals("chrome"))
{
System.setProperty("webdriver.chrome.driver", "F:\\SeleniumLearning\\SeleniumPractice\\Lib\\chromedriver.exe");
driver = new ChromeDriver();
}
else if(browserName.equals("firefox"))
{
System.setProperty("webdriver.chrome.driver", "F:\\SeleniumLearning\\SeleniumPractice\\Lib\\geckodriver.exe");
driver = new FirefoxDriver();
}
else if(browserName.equals("InternetExplorer"))
{
System.setProperty("webdriver.ie.driver", "F:\\SeleniumLearning\\SeleniumPractice\\Lib\\IEDriverServer.exe");
driver = new InternetExplorerDriver();
}
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
return driver;
}
public void URL()
{
driver.get(ReadProperties.readValue("url"));
}
// ************************** Website Operations ********************
public void Login()
{
System.out.println("Should Click on Sign In");
myUtil.returnWebElement("cssSelector", "button[type='button']").click();
myUtil.returnWebElement("name", "lname").sendKeys(ReadProperties.readValue("username"));
myUtil.returnWebElement("cssSelector", ".register__form-field-password").sendKeys(ReadProperties.readValue("password"));
myUtil.returnWebElement("xpath", "//button[@id='button-sign-in']").click();
}
}
`
` SeleniumTest.java
package priceLine.com;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class SeleniumTest {
MainSetUp setUp = new MainSetUp();
@BeforeTest
public void SetBrowserAndUrl()
{
setUp.setUp();
// driver.get(ReadProperties.readValue("url"));
setUp.URL();
}
@Test
public void SearchFlightAndCars()
{
setUp.Login();
}
} `