java - 如何在基类中包含属性文件然后调用其他类?
问题描述
在我的应用程序中,我有属性文件,我在其中添加了输入字段的所有 XPath。通过侦听器类我调用基类,为失败的情况截屏。在 testng.xml 中,我还添加了监听器。每当我运行 testng.xml 时,它都会显示“空指针”,这会使所有测试用例都失败。
注意:在基类中,我有一个初始化函数,其中包含驱动程序、URL、登录详细信息。在其他课程中,我包括了听众。每当我运行时,它都会显示“空指针”。
Base Class:
public class base {
public static WebDriver driver;
public void initialization() throws FileNotFoundException{
Properties prop = new Properties();
FileInputStream fis = new FileInputStream("./src/obj.properties");
try {
prop.load(fis);
} catch (IOException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
driver.get(prop.getProperty("url"));
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
WebElement email = driver.findElement(By.xpath(prop.getProperty("email_path")));
email.sendKeys(prop.getProperty("email"));
email.sendKeys(Keys.TAB);
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
WebElement pass = driver.findElement(By.xpath(prop.getProperty("pwd_path")));
pass.sendKeys(prop.getProperty("pwd"));
WebElement btn = driver.findElement(By.xpath(prop.getProperty("login_btn")));
btn.click();
driver.manage().window().maximize();
}
public void failed(String testmethodname) {
File scrfile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(scrfile,new File("./screenshots/"+testmethodname+".jpg"));
}catch (IOException e) {
e.printStackTrace();
}
}
Listener Class:
public class listener extends base implements ITestListener{
// private WebDriver driver;
public void onTestStart(ITestResult result) {
// TODO Auto-generated method stub
System.out.println("TestCase started and details are "+result.getName());
}
public void onTestSuccess(ITestResult result) {
// TODO Auto-generated method stub
Reporter.log("TestCase Pass");
}
public void onTestFailure(ITestResult result) {
// TODO Auto-generated method stub
System.out.println("Failed Test cases are "+result.getName());
failed(result.getMethod().getMethodName());
Reporter.log("TestCase Fail");
}
Example Class:
@Listeners(listener.class)
public class example extends base{
@BeforeSuite
public void setUp() throws FileNotFoundException {
initialization();
}
@Test(groups={"IV"},enabled = true, priority = 1)
public void exam() throws FileNotFoundException, InterruptedException {
Properties prop = new Properties();
FileInputStream fis = new FileInputStream("./src/obj.properties");
try {
prop.load(fis);
} catch (IOException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
JavascriptExecutor jse22 = (JavascriptExecutor) driver;
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
Thread.sleep(500);
new WebDriverWait(driver, 20)
.until(ExpectedConditions.elementToBeClickable(By.xpath(prop.getProperty("path"))))
.click();
WebElement s = driver.findElement(By.xpath(prop.getProperty("a_path")));
s.click();
Thread.sleep(1000);
}
@AfterMethod
public void tearDown() {
driver.quit();
}
Testng.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
<listeners>
<listener class-name="pkg.listener"/>
</listeners>
<test name="Test">
<groups>
<run>
<include name="IV"/>
</run>
</groups>
<classes>
<class name="pkg.example"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
错误:显示“空指针”异常。
解决方案
它注定会失败。WebDriver driver
永远不会被初始化。在使用 get 方法之前,您需要知道它是哪个驱动程序实例。无论是 Chrome 还是 Firefox 或其他。
步骤如下:
System.setProperty("webdriver.chrome.driver","path//to//chromedriver//");
WebDriver driver = new ChromeDriver();
driver.get(URL);
由于驱动程序从未初始化过,它会Null
在失败时为您提供空指针异常。
推荐阅读
- python-3.x - 根据一列中的条件创建一个新的熊猫列,并从同一数据框中的多列中分配值
- wordpress - Wordpress/Lumberjack - 尝试访问路线时显示 404 页面
- python-3.x - 使用python glob输入文件名片段列表时如何找到所有文件名?
- image - React-native 在同一目录中找不到图像
- python - ConnectionClosedError:在我们收到来自端点 URL 的有效响应之前,连接已关闭:
- python - 针对特定代码的更有效方式的建议
- php - 如何在 html 表格中显示来自 html 表单的数据
- javascript - 我是对的,从对象中破坏对象不会创建新对象
- sabre - Sabre 刷新会话 - OTA_PingRQ 响应显示版本无效
- javascript - 输入选择变量