我是Selenium的新手。!
我使用 Selenium Java 创建了2个类文件,其中尝试实现完全可重用的所有基本功能,我在这里试图从 inputClass.java传递参数到 mainClass.java 到 dropDown(),调用2个方法 chooseElement()和 ddElement()
chooseElement()是 WebElement 而 ddElement()是选择,我需要的只是从 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">
mainClass.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) {
}
}
}
inputClass.java
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();
}
}
答案 0 :(得分:0)
您获得的错误是由于ddElement方法中的以下行。
select.selectByIndex((int) dindex);
您无法将对象强制转换为基本类型int。改为使用Integer。
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) {
}
}
}
InputClass
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();
}
}
ddelement
方法可以包含在dropdown
方法中,因为将其分离是没有优势的。