package selenium.test;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriver.Navigation;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
public class class_test {
public WebDriver driver;
public void startChrome()
{
System.setProperty("webdriver.chrome.driver", "files/chromedriver.exe");
driver=new ChromeDriver();
driver.manage().window().maximize();
}
public void closeChrome()
{
driver.close();
driver.quit();
}
public void goTo(String url)
{
System.out.println(url);
driver.navigate().to(url);
}
public void textInput(String xpath,String text)
{
driver.findElement(By.xpath(xpath)).sendKeys(new String[]{text});
}
public void clearInput(String xpath)
{
driver.findElement(By.xpath(xpath)).clear();
}
public void clickLink(String xpath)//超链接
{
driver.findElement(By.xpath(xpath)).click();
}
public void testOptions(String xpath,String value)//下拉菜单
{
WebElement element=driver.findElement(By.xpath(xpath));//定义一个element来接收下拉列表
Select select=new Select(element);//定义一个select列表对象来获取下拉列表的值
select.selectByValue(value);//注意此处都填小写,填大写会捕捉不到页面元素
select.selectByVisibleText("Volvo");//value不区分大小写,VisbleText区分大小写,
String temp_str=select.getFirstSelectedOption().getText();//First不是列表第一个,是下拉列表当前选中的
System.out.println(temp_str);
List options=select.getOptions();//获取下拉列表
int option_size=options.size();//获取下拉列表大小
System.out.println(option_size);
for(int i=0;i<option_size;i++)
select.selectByIndex(i);//通过获取option_size来完成遍历,也可以随机选择
}
public void testRadiobox(String xpath,int index)//单选框
{
List elements= driver.findElements(By.xpath(xpath));//Webelement对象只能获取一个返回结果,如果要获取多个选项值,需要定义List类型
//需要注意将element列表强制转换为WebElement属性,否则不能调用WebElement的方法,比如click(),identity返回的是多个表项,注意要用findElements而不是findElement方法
WebElement element=(WebElement) elements.get(index);//获取列表中的单个对象进行操作
element.click();
boolean select =element.isSelected();
System.out.println(select);
}
public void testCheckbox(String xpath,int index)//
{
List elements=driver.findElements(By.xpath(xpath));
int list_size=elements.size();
WebElement element=(WebElement) elements.get(index);
element.click();
for(int i=0;i<list_size;i++)
{
element=(WebElement) elements.get(i);
if(element.isSelected())
continue;
else
element.click();
}
}
public void clickButton(String xpath)
{
WebElement element=driver.findElement(By.xpath(xpath));
boolean enabled=element.isEnabled();
if(enabled)
element.click();
else
System.out.println("button is disabled");
}
public void getAttribute(String xpath)
{
WebElement element=driver.findElement(By.xpath(xpath));
String id=element.getAttribute("id");
System.out.println(id);
}
public static void main(String[] args) {
class_test t=new class_test();
t.startChrome();
t.goTo("file:///D:/demo.html");
t.textInput("//input[@id='user']","kuanpeng_tang");
t.clickLink("//a[@class='baidu']");
t.goTo("file:///D:/demo.html");
t.testOptions("//select[@name='select']","audi");
t.testRadiobox("//div[@id='radio']//*[@name='identity']",2);
t.testCheckbox("//*[@type='checkbox']",3);
t.getAttribute("//div[@id='radio']");
t.clickButton("//div[@id='alert']//input[@type='button']");
t.closeChrome();
}
}