public class MyWebdriver extends Webdriver{
private String theParameter;
public MyWebdriver(String parameter){
//... initialize the Webdriver
//store the parameter
theParameter = parameter
}
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 Package 器,在这里我像这样初始化它:
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
}
}
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;
}}
/* 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条答案
按热度按时间lbsnaicq1#
我将做一个假设-您希望将某个参数传递给Webdriver。这可以通过两种方式完成:
1.创建扩展Webdriver的类,并使其构造函数具有您需要传递的参数。但是,这是一种困难的方法,因为您必须实现/覆盖Webdriver中的所有(所需)函数:
1.创建自己的 Package 器,它将包含WebDriver的示例。这很简单。例如:在我自己的测试中,我需要告诉Webdriver我正在测试哪个环境。因此,我为Environment创建了我自己的类:
然后我有了自己的WebDriver Package 器,在这里我像这样初始化它:
而在测试中:
luaexgnf2#
我的建议是尝试使用一个测试框架(TestNG或Junit),它提供了比参数化更多的特性。当你的测试代码增长时,可能在开始时设置框架的一点努力会保存很多努力。
oug3syen3#
并使用property()传递属性文件中的值;在主类中。并执行
ddrv8njm4#
在这段代码中,我们指定Java方法的参数firstName应该接收名为first-name的XML参数的值。〈------〉
有关更多详情,请访问:http://testng.org/doc/documentation-main.html#parameters
rslzwgfq5#
这里我提供了一个可能有用的测试用例
zz2j4svz6#
sq1bmfud7#