我刚刚开始进行自动化,我被困在如何能够以更有效的方式输入不同的xpath,以及我不必使用选择类的3个不同的下拉列表中的日期,月份,年份对于他们中的每一个
以下是代码:
package com.singh.assignment;
import java.io.FileReader;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
public class Json
{
public static void main(String args[])
{
JsonParser parser = new JsonParser();
try
{
Object obj = parser.parse(new FileReader("C:\\Users\\dell\\eclipse-
workspace\\Assignment\\data.json"));
JsonObject jsonObject = (JsonObject) obj;
String fname = (String) jsonObject.get("fname").getAsString();
String lname = (String) jsonObject.get("lname").getAsString();
String baseurl = (String) jsonObject.get("baseurl").getAsString();
String mstatus = (String) jsonObject.get("mstatus").getAsString();
System.setProperty("webdriver.gecko.driver","E:\\WORK\\geckodriver.exe\\");
WebDriver driver = new FirefoxDriver();
driver.get(baseurl);
driver.findElement(By.partialLinkText("Registration")).click();
driver.findElement(By.xpath("//input[@id =
'name_3_firstname']")).sendKeys(fname);
driver.findElement(By.xpath("//input[@id =
'name_3_lastname']")).sendKeys(lname);
List<WebElement> martial = driver.findElements(By.name("radio_4[]"));
{
for(WebElement radio : martial)
{
if(radio.getAttribute("value").equalsIgnoreCase(mstatus))
{
radio.click();
}
}
}
driver.findElement(By.xpath("//input[@value = 'reading']")).click();
WebElement cntry = driver.findElement(By.xpath("//select[@id =
'dropdown_7']"));
Thread.sleep(3000);
Select index = new Select(cntry);
index.selectByVisibleText("India");
WebElement month = driver.findElement(By.id("mm_date_8"));
Select index1 = new Select(month);
index1.selectByVisibleText("9");
WebElement date = driver.findElement(By.id("dd_date_8"));
Select index2 = new Select(date);
index2.selectByVisibleText("15");
WebElement year = driver.findElement(By.id("dd_date_8"));
Select index3 = new Select(year);
index3.selectByVisibleText("1995");
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
这是JSON文件:
{
"baseurl": "http://demoqa.com/",
"fname": AKASHDEEP,
"lname": SINGH,
"mstatus": single,
"hobby": ["Dance", "Reading", "Cricket"]
}
答案 0 :(得分:1)
我说你需要完全修改你的测试&#39;实施方法。理想的测试用例不应该对WebDriver,定位器或硬编码数据有所了解。您应该尝试构建多个抽象层来封装框架内的驱动程序调用,页面对象中的定位器,外部存储中的测试数据(以及实体和数据提供程序)。
如果我们谈谈您的特定场景的一些基本优化,我首先要创建一些抽象页面,这可能会隐藏与WebDriver的显式交互:
public abstract class AbstractPage {
private final WebDriverWait wait;
public AbstractPage() {
// assuming some external driver provider
this.wait = new WebDriverWait(getDriver(), 10);
}
public void selectByVisibleText(final By locator, final String text) {
new Select(waitFor(locator, ExpectedConditions::visibilityOfElementLocated)).selectByVisibleText(text);
}
public void selectByVisibleText(final By locator, final int number) {
selectByVisibleText(locator, String.valueOf(number));
}
private WebElement waitFor(final By locator, final Function<By, ExpectedCondition<WebElement>> condition) {
return wait.until(condition.apply(locator));
}
}
然后你可以为你的域逻辑创建一个页面对象,它将重用常见的下拉选择方法:
public class HomePage extends AbstractPage {
private By dropdownDate = By.id("date");
private By dropdownMonth = By.id("month");
private By dropdownYear = By.id("year");
public HomePage selectDate(final LocalDate date) {
selectByVisibleText(dropdownMonth, date.getMonthValue());
selectByVisibleText(dropdownDate, date.getDayOfMonth());
selectByVisibleText(dropdownYear, date.getYear());
return this;
}
}
在您的测试用例中,您只需调用selectDate(date)
,这是表达业务逻辑的更简洁和人类可读的方式。