Java 如何组合Webelement并选择以使其具有可重用的功能。?
我是硒的新手强> 我使用Selenium Java创建了两个类文件,试图实现完全可重用的所有基本功能,我在这里尝试将参数从inputClass.Java传递到mainClass.Java到dropDown()内,调用两个方法chooseElement()和dElement() 如果chooseElement()是WebElement而ddeElement()是Select,我所需要的就是从inputClass.java传递值以使下拉菜单正常工作。有人在我的代码里纠正我 提前谢谢 页面源代码:Java 如何组合Webelement并选择以使其具有可重用的功能。?,java,selenium,selenium-webdriver,selenium-chromedriver,Java,Selenium,Selenium Webdriver,Selenium Chromedriver,我是硒的新手 我使用Selenium Java创建了两个类文件,试图实现完全可重用的所有基本功能,我在这里尝试将参数从inputClass.Java传递到mainClass.Java到dropDown()内,调用两个方法chooseElement()和dElement() 如果chooseElement()是WebElement而ddeElement()是Select,我所需要的就是从inputClass.java传递值以使下拉菜单正常工作。有人在我的代码里纠正我 提前谢谢 页面源代码: <
<div id="autocomplete_chosen" class="chosen-container chosen-container-single" style="width: 300px;" title="">
<a class="chosen-single" tabindex="-1">
<span>Selenium</span>
<div>
<b/>
</div>
</a>
<div class="chosen-drop">
inputClass.java
public class methods {
static WebDriver driver;
public static void wait(int w) throws InterruptedException {
driver.manage().timeouts().implicitlyWait(w, TimeUnit.SECONDS);
}
public static Actions getAction() {
Actions action = new Actions(driver);
return action;
}
public static WebElement chooseElement(int x,String path){
WebElement webElement = null;
switch (x){
case 1:
webElement=driver.findElement(By.id(path));
break;
case 2:
webElement=driver.findElement(By.className(path));
break;
case 3:
webElement=driver.findElement(By.linkText(path));
break;
case 4:
webElement=driver.findElement(By.xpath(path));
break;
case 5:
webElement=driver.findElement(By.cssSelector(path));
break;
case 6:
webElement = driver.findElement(By.tagName(path));
break;
}
return webElement;
}
public static Select ddElement(WebElement webElement, int dx,Object dindex){
Select select = new Select(webElement);
switch (dx){
case 1:
System.out.println("case 1");
select.selectByVisibleText((String) dindex);
break;
case 2:
System.out.println("case 2");
select.selectByValue((String) dindex);
break;
case 3:
System.out.println("case 3");
select.selectByIndex((int) dindex);
break;
}
return select;
}
public static void mouseOver(int x, String path) throws InterruptedException {
WebElement mO=chooseElement(x, path);
getAction().moveToElement(mO).perform();
}
public static void textBox (int x, String path, String text) throws InterruptedException {
chooseElement(x, path).sendKeys(text);
getAction().sendKeys(Keys.ESCAPE);
}
public static void click(int x, String path) throws InterruptedException {
chooseElement(x, path).click();
}
public static String getTxt(int x, String path) throws InterruptedException {
String returnText = chooseElement(x, path).getText();
return returnText;
}
public static void dropDown(int x, String path, int dx,Object dindex) throws InterruptedException {
try {
WebElement webElement=chooseElement(x, path);
ddElement(webElement,dx,dindex); // Value index
}
catch (NoSuchElementException e) {
}
}
}
public class OrderRabbit extends methods {
@Test
public static void main (String arg []) throws IOException, InterruptedException{
System.setProperty("webdriver.chrome.driver", "C:\\selenium-2.47.1\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.qatest.com/");
// Login
mouseOver(4, ".//*[@id='Login_block']/a/span");
getAction().sendKeys(Keys.ESCAPE);
wait(60);
textBox(1, "username","******@gmail.com");
textBox(1, "password","******");
click(4, ".//*[@id='loginForm']/button");
//Assertion
/*String str = getTxt(1, "firstNameIdProfile");
Assert.assertNotEquals(str,"Login");*/
Thread.sleep(2000);
//Click DropDown
//click(1,"autocomplete");
dropDown(1,"autocomplete_chosen",1 ,"Course");
driver.quit();
}
}
您得到的错误是由于ddElement方法中的以下行引起的
select.selectByIndex((int) dindex);
无法将对象强制转换为基元类型int。请改用整数
select.selectByIndex(((Integer) dindex).intValue());
但是,编写方法类的更好方法如下所示
主类
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;
public class Methods {
protected static WebDriver driver;
protected static final int ID = 1;
protected static final int CLASS = 2;
protected static final int LINKTEXT = 3;
protected static final int XPATH = 4;
protected static final int CSS = 5;
protected static final int TAGNAME = 6;
protected static final int VISIBLETEXT = 1;
protected static final int VALUE = 2;
protected static final int INDEX = 3;
public static void wait(int timeOutInSeconds) throws InterruptedException {
driver.manage().timeouts().implicitlyWait(timeOutInSeconds, TimeUnit.SECONDS);
}
public static Actions getAction() {
Actions action = new Actions(driver);
return action;
}
private static WebElement chooseElement(int byStrategy, String locatorValue) {
By by = null;
switch (byStrategy) {
case ID:
by = By.id(locatorValue);
break;
case CLASS:
by = By.className(locatorValue);
break;
case LINKTEXT:
by = By.linkText(locatorValue);
break;
case XPATH:
by = By.xpath(locatorValue);
break;
case CSS:
by = By.cssSelector(locatorValue);
break;
case TAGNAME:
by = By.tagName(locatorValue);
break;
}
return driver.findElement(by);
}
public static void mouseOver(int byStrategy, String locatorValue) throws InterruptedException {
WebElement mO = chooseElement(byStrategy, locatorValue);
getAction().moveToElement(mO).perform();
}
public static void textBox(int byStrategy, String locatorValue, String text) throws InterruptedException {
chooseElement(byStrategy, locatorValue).sendKeys(text);
getAction().sendKeys(Keys.ESCAPE);
}
public static void click(int byStrategy, String locatorValue) throws InterruptedException {
chooseElement(byStrategy, locatorValue).click();
}
public static String getTxt(int byStrategy, String locatorValue) throws InterruptedException {
String returnText = chooseElement(byStrategy, locatorValue).getText();
return returnText;
}
public static void dropDown(int byStrategy, String locatorValue, int selectStrategy, Object strategyValue)
throws InterruptedException {
try {
WebElement webElement = chooseElement(byStrategy, locatorValue);
Select select = new Select(webElement);
switch (selectStrategy) {
case VISIBLETEXT:
System.out.println("case 1");
select.selectByVisibleText((String) strategyValue);
break;
case VALUE:
System.out.println("case 2");
select.selectByValue((String) strategyValue);
break;
case INDEX:
System.out.println("case 3");
select.selectByIndex(((Integer) strategyValue).intValue());
break;
}
} catch (NoSuchElementException e) {
}
}
}
输入类
import java.io.IOException;
import org.openqa.selenium.Keys;
import org.openqa.selenium.chrome.ChromeDriver;
public class OrderRabbit extends Methods {
public static void main(String arg[]) throws IOException, InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:\\selenium-2.47.1\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.qatest.com/");
// Login
mouseOver(XPATH, ".//*[@id='Login_block']/a/span");
getAction().sendKeys(Keys.ESCAPE);
wait(60);
textBox(ID, "username", "******@gmail.com");
textBox(ID, "password", "******");
click(XPATH, ".//*[@id='loginForm']/button");
Thread.sleep(2000);
// Click DropDown
// click(ID, "autocomplete");
dropDown(ID, "autocomplete_chosen", VISIBLETEXT, "Course");
driver.quit();
}
}
dElement
方法可以包含在下拉列表中,因为将它分离出来没有任何好处。嘿,我想你可以再使用一行,比如WebElement myEle=chooseElement(int x,String path);之后进行下拉(myEle,1,“课程”);请正确地检查参数并尝试一下,这样您就可以将一些简单易读的东西,如driver.findElement(By.Id(“someId”)
变成一些神秘的东西,如`chooseElement(1,“someId”)。。。为什么?在这种情况下,没有理由为核心API创建包装器。您真的应该花些时间阅读一些基本的Java/编程教程。@JeffC,有些公司更喜欢提供关键字驱动的自动化框架。这样,错误处理和日志记录就可以轻松处理,编程知识较少的人也可以创建简单的脚本。@sigil:谢谢,这很有效well@JeffC当前位置谢谢你的建议,我会记住的。仅供参考:我已经提到我是新手。我刚进入这个论坛学习编程作为第一步,请接受答案,这样问题就可以结束了