[英]Can we pass object of a List<WebElement> to a function as a parameter?
[英]Is there a way to pass a webelement as a parameter?
我正在使用包含三個 ID 定位器的頁面 object 構建三個測試用例。 我想知道是否可以將定位器傳遞給所有三種情況的單個方法。
頁 Object:
public class SomeClass {
@FindBy(how=How.CSS, using="label[for='yes']")
private WebElement yes;
@FindBy(how=How.CSS, using="label[for='maybe']")
private WebElement maybe;
@FindBy(how=How.CSS, using="label[for='no']")
private WebElement no;
public SomeClass(WebDriver driver) {
super(driver);
}
public SomeClass clicksButtons() {
*some locator*.click();
return new someClass(this.driver);
}
}
測試用例:
public class SomeTest {
@Test
public void willClickAButton() {
SomeClass someClass = new SomeClass(this.getDriver());
SomeClass.clicksButtons();
Assert.assertTrue(true);
}
我想將一個參數(是、可能或否)傳遞給 clicksButtons 方法,以便我可以在其他兩個測試用例中重用該方法,而無需對其進行硬編碼。 我搜索了谷歌並找不到明確的答案。
這個
public SomeClass clicksButtons() {
*some locator*.click();
return new someClass(this.driver);
}
應該:
public SomeClass clicksButtons(WebElemenet ele) {
ele.click();
return new someClass(this.driver);
}
並像這樣調用這個方法:
SomeClass.clicksButtons(pass a webelement here);
我在 Selenium 測試工具食譜第 2 版中找到了答案。
public void select(String label) {
//I found the buttons by class and stored them in a List<>. In my case there were 3.
List<WebElement> like = this.driver.findElements(By.className("custom-control-label"));
//iterate the buttons contained in the List<>
for(WebElement button : like)
{
//if the button's "for" attribute contains the given label...
if (button.getAttribute("for").equals(label))
{
//...and if it is not selected...
if (!button.isSelected())
{
//click the button, according to the label
button.click();
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.