如何在Selenium 2(WebDriver)中进行参数化?我使用Eclipse和maven插件,我以前没有使用Selenium Webdriver。当我谷歌为它,然后每件事显示testNG和JUnit。我们可以通过哪种方法参数化Webdriver吗?
答案 0 :(得分:0)
我要做出假设 - 你想将一些参数传递给Webdriver。这可以通过两种方式完成:
创建扩展Webdriver并使其构造函数具有您需要传递的参数的类。但是,这是 hard 方式,因为你必须从webdriver实现/覆盖所有(需要的)函数:
public class MyWebdriver extends Webdriver{
private String theParameter;
public MyWebdriver(String parameter){
//... initialize the Webdriver
//store the parameter
theParameter = parameter
}
制作自己的包装器,它将包含WebDriver的实例。那是容易( - ier)。例如:在我自己的测试中,我需要告诉Webdriver我正在测试哪个环境。所以我为环境创建了自己的类:
public class Environment{
private String baseUrl;
public enum NameOfEnvironment {DEV, ACC}
private NameOfEnvironment environment;
public Environment(NameOfEnvironment envName){
environment = envName;
}
public String getBaseUrl(){
switch (environment){
case DEV: baseUrl = "https://10.10.11.12:9080/test/";
break;
case ACC: baseUrl = "https://acceptance.our-official-site.com";
break;
}
return baseUrl;
}
}
然后我有自己的WebDriver包装器,我在这里初始化它:
public class TestUI{
private Webdriver driver;
private Environment env;
public TestUI(Environment e){
this.env = e;
driver = new FirefoxDriver;
driver.get(env.getBaseUrl());
}
}
在测试中:
public class TestCases{
public static final Environment USED_ENVIRONMENT = new Environment(Environment.NameOfEnvironment.ACC);
@Test
public void testSomething(){
testUI test = new testUI(USED_ENVIRONMENT);
//.. further steps
}
}
答案 1 :(得分:0)
我的建议是尝试使用测试框架(TestNG或Junit),它提供了比参数化更多的功能。在测试代码增长时,在开始时设置框架可能需要花费很多精力。
答案 2 :(得分:0)
public void property(){
try {
File file = new File("login.properties");
FileInputStream fileInput = new FileInputStream(file);
Properties properties = new Properties();
properties.load(fileInput);
fileInput.close();
Enumeration enuKeys = properties.keys();
while (enuKeys.hasMoreElements()) {
String key = (String) enuKeys.nextElement();
String value = properties.getProperty(key);
driver.findElement(By.id(key)).sendKeys(value);
System.out.println(key + ": " + value);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
并传递属性文件中的值使用property();在主要班级。并执行
答案 3 :(得分:0)
@Parameters({ "first-name" })
@Test
public void testSingleString(String firstName) {
System.out.println("Invoked testString " + firstName);
assert "Cedric".equals(firstName);
}
在此代码中,我们指定Java方法的参数firstName应该接收名为first-name的XML参数的值。此XML参数在testng.xml中定义: < - ... - >
有关详细信息,请访问以下内容: http://testng.org/doc/documentation-main.html#parameters
答案 4 :(得分:0)
我在这里提供一个可能有用的测试用例
package pac1;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Wait;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.*;
public class test extends sut {
static WebDriver driver;
static Wait<WebDriver> wait;
public static boolean run(WebDriver driverArg, Wait<WebDriver> waitArg)
{
driver = driverArg;
wait = waitArg;
// Run all the methods and return false if any fails
return (test1() );
}
private static boolean test1()
{
driver.get("https://accounts.google.com");
try {
File file = new File("emaildata.xml"); //file location should be specified correctly put your xml in the same folder of the source code.
// Prepare XML
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(file);
document.getDocumentElement().normalize();
NodeList emailNodeElementList = document.getElementsByTagName("test");//test is the name of the child tag
for(int j=0;j<emailNodeElementList.getLength(); j++)//loop for the multiple data
{
String client = "The username or password you entered is incorrect. ?";
Element emailNodeElement = (Element)emailNodeElementList.item(j);
NodeList details = emailNodeElement.getChildNodes();
String emailAddress=((Node) details.item(0)).getNodeValue();
System.out.println("email :" + emailAddress);//it just prints which email is going to be parsed
WebElement element = driver.findElement(By.cssSelector("body"));
boolean feedBack = driver.findElement(By.cssSelector("body")).getText().contains(client);
boolean feedbackVisible = element.isDisplayed();
WebElement e1 = driver.findElement(By.id("Email"));//getting the location from the web
e1.sendKeys(emailAddress);//sending keys to the server
WebElement e3 = driver.findElement(By.id("signIn"));
e3.click();
if(feedBack==true){
System.out.println(client+ "is present");
if(feedbackVisible==true){
System.out.println(client+ "is visible");
}
else{
System.out.println(client+ "is not visible");
}
}
else{
System.out.println(client+ "is not present");
}
}
}
catch (Exception e) {e.printStackTrace();}
return true;
}}
答案 5 :(得分:0)
您可以使用Apache或Jexcel的POI软件。
请查看下面的这些链接,它可能对您有帮助;
对于POI
http://viralpatel.net/blogs/java-read-write-excel-file-apache-poi/
和JExcel http://www.youtube.com/watch?v=yOGGdv8eT80
我希望它可以帮助你。我也不是专业人士,但在尝试学习参数化时,我用Google搜索并找到了这些链接。
答案 6 :(得分:0)
/* You can pass parameters by creating two classes.
The first class, which you can call Main Class, will be public static void.
It will contain code such as:
*/
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.com");
Parameters id = new Parameters(); //this is referring to second class
driver.findElement(By.name("q")).sendKeys(id.x);
//****************************************************************
//this is the second class [separate page] - here you can declare int or string varibale as public [so you can share them with other class while defining them] -
//when you make this class - don't need to add public static void
public String x = "username"
//if you look at the first page, I'm passing this string in the main script
答案 7 :(得分:0)
public class ReadExcel {
public String readData (String strSheetName, int strRowNo, int strCellNo) throws Exception {
FileInputStream fis = new FileInputStream("D:\\Selenium\\TestData.xlsx");
Workbook wb = WorkbookFactory.create(fis);
Sheet sh = wb.getSheet(strSheetName);
Row rw = sh.getRow(strRowNo);
String val = rw.getCell(strCellNo).getStringCellValue();
return val;
}
}