我是学习Selenium WebDriver和testNG的新手。我试图通过下面的一组测试用例学习一些testNG注释,但是一个测试用例总是失败而且我无法弄清楚错误是什么?
此外,我尝试搜索现有问题,但无法找到与我的问题匹配的内容。所以我在这里解释一下我想要实现的目标:
我创建了三个包含testNG
注释测试用例的类。类名称为:testingNG
,testingNG1
和testingNG2
。
然后我创建了一个testNg xml文件,以便这些测试用例可以作为诉讼执行。
我还创建了一个属性文件(paths.properties
),它存储了我想在test2
类testingNG
中使用的一些xpath。
当我将这个xml文件作为套装运行时,test2
类的testingNG
总是失败,但是eclipse没有显示任何语法错误,我无法理解问题是什么
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class testingNG {
@Test
public void test1()
{
System.out.println("first NG test case");
}
@Test
public void test2() throws IOException, InterruptedException
{
Properties pro=new Properties();
FileInputStream fis= new FileInputStream (System.getProperty("C:\\Users\\SumitKumar.Dubey\\workspace\\My_testNG\\src\\paths.properties"));
pro.load(fis);
System.out.println("Login link xpath: "+pro.getProperty("DLJ_Login_Button"));
FirefoxDriver driver=new FirefoxDriver();
driver.get("http://www.dsij.in/");
driver.findElement(By.xpath(pro.getProperty("DLJ_Login_Button"))).click();
Thread.sleep(3000);
driver.findElement(By.xpath(pro.getProperty("DLJ_Login_Username"))).sendKeys("dubey_sumit");
driver.findElement(By.xpath(pro.getProperty("DLJ_Login_Pwd"))).sendKeys("0VUoUEMGpiC");
driver.findElement(By.xpath(pro.getProperty("DLJ_click_login"))).click();
}
}
Second class: testingNG1
=========================
import org.testng.annotations.Test;
public class testingNG1 {
@Test
public void test4()
{
System.out.println("fourth NG test case from testNG1 class");
}
}
import java.io.Console;
import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.Test;
public class testingNG2 {
FirefoxDriver driver=new FirefoxDriver();
@Test (priority=1)
public void site_navigate()
{
driver.get("http://demoqa.com/");
System.out.println("print when ulr is entered!");
}
@Test (priority=2)
public void click_about_us()
{
driver.findElement(By.xpath("//a[@href='http://demoqa.com/about-us/']")).click();
System.out.println("print when ulr is entered!");
//driver.getPageSource().contains("About us");
if(driver.getPageSource().contains("About us"))
{
System.out.println("Text is Present");
}
else
{
System.out.println("Text is not Present");
}
}
@AfterTest
public void close_browser()
{
driver.close();
driver.quit();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test name="Test1">
<classes>
<class name="testingNG1"/>
</classes>
</test>
<test name="Test2">
<classes>
<class name="testingNG2"/>
</classes>
</test>
<test name="Test3">
<classes>
<class name="testingNG"/>
</classes>
</test>
</suite> <!-- Suite -->
#Dalal Street Login
DLJ_Login_Button=//a[@id='dnn_dnnLogin_enhancedLoginLink']
DLJ_Login_Username=//input[@id='dnn_ctr5070_Login_Login_DNN_txtUsername']
DLJ_Login_Pwd=//input[@id='dnn_ctr5070_Login_Login_DNN_txtPassword']
DLJ_click_login=//input[@id='dnn_ctr5070_Login_Login_DNN_cmdLogin']
此test2
类的testingNG
失败,使用属性文件。
答案 0 :(得分:1)
我认为由于这一行而没有加载属性:
FileInputStream fis= new FileInputStream (System.getProperty("C:\\Users\\SumitKumar.Dubey\\workspace\\My_testNG\\src\\paths.properties"));
请从此行中删除System.getProperty()并在File对象中指定路径,如下所示。
FileInputStream fis= new FileInputStream (new File("C:\\Users\\SumitKumar.Dubey\\workspace\\My_testNG\\src\\paths.properties"));