您的位置:首页 > 其它

自学selenium第五节-selenium各种类的使用

2015-06-07 18:18 597 查看
package selenium.test;

import java.util.List;
import java.util.Set;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

public class class5 {

public WebDriver driver;
public void startFirefox()
{
driver =new FirefoxDriver();
driver.manage().window().maximize();
}
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 test_alert(String xpath)
{
WebElement element=driver.findElement(By.xpath(xpath));
Actions action=new Actions(driver);
action.click(element).perform();//action类的使用
Alert alert=driver.switchTo().alert();
String text=alert.getText();
System.out.println(text);
alert.accept();
//alert.dismiss();取消操作
}
public void testAction(String xpath)
{
WebElement element=driver.findElement(By.xpath(xpath));
Actions action=new Actions(driver);
String actionText = driver.findElement(By.id("over")).getText();//还没有进行JS注入,div[@id='over']中值为空
System.out.println(actionText);
action.moveToElement(element).perform();
actionText = driver.findElement(By.id("over")).getText();
System.out.println(actionText);
}
public void upload(String xpath,String filename)
{
WebElement element=driver.findElement(By.xpath(xpath));
element.sendKeys(filename);//把需要上传的文件赋值到上床到框体
}
public void testjs()
{
//driver.get("http://www.haosou.com/");
//String ret=(String) ((JavascriptExecutor)driver).executeScript("return document:getElementById('search-button').value;");
//System.out.println(ret);

JavascriptExecutor j=(JavascriptExecutor)driver;
j.executeScript("alert('鹁鸽萨比')");
//System.out.println("1111");
}
public void testIframe()
{
driver.findElement(By.id("user")).sendKeys("Tang");//页面原框体
driver.switchTo().frame("aa");
driver.findElement(By.id("user")).sendKeys("MaXiaochu");//frame内框体
driver.switchTo().defaultContent();//切换回原页面
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
driver.findElement(By.id("user")).clear();
driver.findElement(By.id("user")).sendKeys("TangKuanpeng");//页面原框体
}
public void testSwitchWindows()
{
WebElement element =driver.findElement(By.xpath("//a[@class='open']"));
element.click();//激活页面
Set <String> handles=driver.getWindowHandles();//得到的是所有页面的句柄列表
String handle =driver.getWindowHandle();//得到的是最初默认窗体的句柄
handles.remove(handle);//不执行这一句,handle和newhandle回相同
String newhandle=handles.iterator().next();
//driver.findElement(By.id("kw")).sendKeys("TangKuanpeng");
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
driver.switchTo().window(handle);//切回原窗口
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
driver.switchTo().window(newhandle);//切回新窗体
driver.close();
//System.out.println(handle);
//System.out.println(newhandle);//拿到的newhandle和handle是一个值

}
public void testWait()
{
WebElement element=driver.findElement(By.className("wait"));
element.click();
boolean wait=new WebDriverWait(driver,10).until
(
new ExpectedCondition<Boolean>()
{
public Boolean apply(WebDriver driver){
return driver.findElement(By.className("red")).isDisplayed();
}
}
);
System.out.println(wait);
element=driver.findElement(By.className("red"));
String wait_text=element.getText();
System.out.println(wait_text);
}
public static void main(String[] args) {
class5 t=new class5();
//t.startChrome();
//t.goTo("http://www.baidu.com");
t.startFirefox();
t.goTo("file:///D:/demo_new/demo_new.html");
t.test_alert("//input[@class='alert']");
t.testAction("//input[@value='Action']");
t.upload("//input[@id='load']","D:\\test.txt");
//t.testjs();
//t.testIframe();
//t.testSwitchWindows();
t.testWait();
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: