Java 我不知道';我不想一次又一次地编写driver.findelement(By.xpath(";))
您好,这是下面的代码:我要做的是构建一个函数,在其中我只传递XPath的值,这样我就不必反复编写Java 我不知道';我不想一次又一次地编写driver.findelement(By.xpath(";)),java,selenium-webdriver,Java,Selenium Webdriver,您好,这是下面的代码:我要做的是构建一个函数,在其中我只传递XPath的值,这样我就不必反复编写driver.findelelement(By.XPath(“”) driver.findElement(By.xpath("//*[@id='lead_source']")).sendKeys("Existing Customer"); driver.findElement(By.xpath("//*[@id='date_closed']")).sendKeys("08/07/2013"); dri
driver.findelelement(By.XPath(“”)
driver.findElement(By.xpath("//*[@id='lead_source']")).sendKeys("Existing Customer");
driver.findElement(By.xpath("//*[@id='date_closed']")).sendKeys("08/07/2013");
driver.findElement(By.xpath("//*[@id='sales_stage']")).sendKeys("Opportuntiy Qualification");
driver.findElement(By.xpath("//*[@id='opportunity_monthly_volume']")).sendKeys("10895");
driver.findElement(By.xpath("//*[@id='probability']")).sendKeys("90");
driver.findElement(By.xpath("//*[@id='opportunity_sales_rep']")).sendKeys("Sales Rep");
driver.findElement(By.xpath("//*[@id='opportunity_sales_regions']")).sendKeys("Northeast");
driver.findElement(By.xpath("//*[@id='opportunity_current_lab']")).sendKeys("Current lab");
driver.findElement(By.cssSelector(Payermixcss +"opportunity_medicare")).sendKeys("5");
将方法提取到上层,并将值作为参数传递 例如: yourMethod(路径){ driver.findElement(By.xpath(path))
}将方法提取到上层,并将值作为参数传递 例如: yourMethod(路径){ driver.findElement(By.xpath(path))
}根据您的关注点,您可以使用页面对象模型创建方法,并将变量传递给精确方法。。我不懂java,但我知道java和概念
private string variable= "Xpath value"
将此变量传递给方法,它将与POM交互。。在此之前,你应该了解POM。这样你就可以很容易地理解这些概念。希望它能帮助你 根据您的关注点,您可以使用页面对象模型创建方法,并将变量传递给精确方法。。我不懂java,但我知道java和概念
private string variable= "Xpath value"
将此变量传递给方法,它将与POM交互。。在此之前,你应该了解POM。这样你就可以很容易地理解这些概念。希望它能帮助你 最好的方法是使用该模式。你可以这样做:
public class MyFormPageObject {
public MyFormPageObject enterLeadSource(String value) {
driver.findElement(By.id("lead_source")).sendKeys(value);
return this;
}
public MyFormPageObject enterDateClosed(String value) {
driver.findElement(By.id("date_closed")).sendKeys(value);
return this;
}
//...
}
// then in your test code
myFormPO.enterLeadSource("Existing Customer").enter("08/07/2013");
private WebElement findElementByXpath(String xpath) {
return driver.findElement(By.xpath(xpath));
}
请注意,如前所述,如果您有标识符,那么应该使用
By.id
,因为XPath速度较慢,而且并非总是得到WebDriver
的所有实现的很好支持,最好的方法是使用该模式。你可以这样做:
public class MyFormPageObject {
public MyFormPageObject enterLeadSource(String value) {
driver.findElement(By.id("lead_source")).sendKeys(value);
return this;
}
public MyFormPageObject enterDateClosed(String value) {
driver.findElement(By.id("date_closed")).sendKeys(value);
return this;
}
//...
}
// then in your test code
myFormPO.enterLeadSource("Existing Customer").enter("08/07/2013");
private WebElement findElementByXpath(String xpath) {
return driver.findElement(By.xpath(xpath));
}
请注意,如前所述,如果您有标识符,则应使用
By.id
,因为XPath速度较慢,而且并非总是得到WebDriver的所有实现的良好支持
要减少必须编写的代码量,可以使用如下函数:
public class MyFormPageObject {
public MyFormPageObject enterLeadSource(String value) {
driver.findElement(By.id("lead_source")).sendKeys(value);
return this;
}
public MyFormPageObject enterDateClosed(String value) {
driver.findElement(By.id("date_closed")).sendKeys(value);
return this;
}
//...
}
// then in your test code
myFormPO.enterLeadSource("Existing Customer").enter("08/07/2013");
private WebElement findElementByXpath(String xpath) {
return driver.findElement(By.xpath(xpath));
}
代码的第一行是:
findElementByXpath("//*[@id='lead_source']").sendKeys("Existing Customer");
它实际上并没有减少代码的长度,但在Eclipse IDE中自动完成只需要一个CTRL+空格。要减少必须编写的代码量,可以使用如下函数:
public class MyFormPageObject {
public MyFormPageObject enterLeadSource(String value) {
driver.findElement(By.id("lead_source")).sendKeys(value);
return this;
}
public MyFormPageObject enterDateClosed(String value) {
driver.findElement(By.id("date_closed")).sendKeys(value);
return this;
}
//...
}
// then in your test code
myFormPO.enterLeadSource("Existing Customer").enter("08/07/2013");
private WebElement findElementByXpath(String xpath) {
return driver.findElement(By.xpath(xpath));
}
代码的第一行是:
findElementByXpath("//*[@id='lead_source']").sendKeys("Existing Customer");
它并没有真正减少代码的长度,但在Eclipse IDE中自动完成只需要一个CTRL+空格。仍然无法做到这一点,如果您能详细说明一下,这会有所帮助。因为我是新手。@Devraj您的问题与Selenium无关,而是与编程有关。首先了解一个方法/函数的目标是什么。@LaurentG同意您的意见\m/仍然无法做到这一点,如果您能详细说明一下,这将有所帮助。因为我是新手。@Devraj您的问题与Selenium无关,而是与编程有关。首先了解方法/函数的目标是什么。@LaurentG同意您的意见\m/I建议您使用By.id而不是By.xpath。我建议您使用By.id而不是By.xpath。老实说,我不喜欢这个。这并不更糟,但毫无好处
findElement()
允许多个重载,而这个重载只接受xpath。有些人可能需要更多的方法,如getElementByCssSelector
等。唯一的优点是使用的字符更少,但这并不能解决正确的问题。OP真正需要的是一个好的设计。这个问题的解决方案应该是使用,就像Arran的答案(PageObjects部分)。这回答了我想做的问题是构建一个函数。。。。但我同意,最好的解决方案是使用PageObject模式。同样,使用此模式,您需要多次调用驱动程序,然后使用一种常用方法将调用包装到WebDriver
。感谢您的解释。你说服了我。我将编辑我的答案。@LaurentG您应该单独发布页面对象答案;在同一个答案中有两个完全不同的解决方案有点让人困惑——我想对其中一个进行投票,而不是另一个。我最初想这样做,但我不能删除我的第一个答案,因为它被接受。老实说,我不喜欢这个。这并不更糟,但毫无好处findElement()
允许多个重载,而这个重载只接受xpath。有些人可能需要更多的方法,如getElementByCssSelector
等。唯一的优点是使用的字符更少,但这并不能解决正确的问题。OP真正需要的是一个好的设计。这个问题的解决方案应该是使用,就像Arran的答案(PageObjects部分)。这回答了我想做的问题是构建一个函数。。。。但我同意,最好的解决方案是使用PageObject模式。同样,使用此模式,您需要多次调用驱动程序,然后使用一种常用方法将调用包装到WebDriver
。感谢您的解释。你说服了我。我将编辑我的答案。@LaurentG您应该单独发布页面对象答案;在同一个答案中有两个完全不同的答案有点让人困惑——我想对其中一个进行投票,而不是对另一个进行投票。我首先想这样做,但我不能删除我的第一个答案,因为它被接受了。