Selenium常用函数封装

package com.wiley.enterprise;

import java.io.IOException;
import java.util.List;
import java.util.NoSuchElementException;

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.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.sikuli.script.FindFailed;
import org.sikuli.script.Pattern;
import org.sikuli.script.Screen;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;


public class BaseTest {

	protected WebDriver driver;
	
	@BeforeMethod
	public void initCase(){
		System.setProperty("webdriver.chrome.driver", "E:\\webdriver\\chromedriver.exe");
		driver = new ChromeDriver();
		driver.manage().window().maximize();
		driver.get("http://112.5.161.79:91/login.html");
		this.login("182xxx2116", "xxxxxx");
	}
	
	/**
	 * login with mobile and pwd
	 * @param mobile
	 * @param pwd
	 */
	public void login(String mobile, String pwd){
		this.sendKeys(By.name("mobile"), mobile);
		this.sendKeys(By.name("password"), pwd);
		this.clickEle(By.xpath("//button[@type='submit']"));
	}
	
	@AfterMethod
	public void quit(){
		if(driver != null){
			driver.quit();
		}
	}
	
	/**
	 * call autoIt3 to upload the file
	 * @throws InterruptedException 
	 * @throws FindFailed 
	 */
	public void uploadFile(By loc, String path) throws InterruptedException, FindFailed{
		//等待弹出框
		Thread.sleep(2000);
		//sikuli模式
		Screen screen = new Screen();
		Pattern camera = new Pattern(path);
		screen.click(camera);
        try {
            Runtime.getRuntime().exec("E:\\test\\upload.exe");
        } catch (IOException e) {
        	System.err.println("调用autoit3失败!");
        }
        Thread.sleep(2000);
        WebElement sureButton = null;
        try{
        	sureButton = this.findEle(loc);
        	Thread.sleep(5000);
        	sureButton.click();
        }catch(Exception e){
        	System.err.println("定位不到该元素!");
        }
	}
	
	/**
	 * select by value
	 * @param loc
	 * @param value
	 */
	public void selectByValue(String loc, String value){
		Select type = new Select(this.findEle(By.name(loc)));
		type.selectByValue(value);
	}
	
	/**
	 * roll down and keep the element to the center of browser
	 * @param loc
	 */
	public void scrollToElement(By loc){
		WebElement ele = this.findEle(loc);
		JavascriptExecutor js = (JavascriptExecutor) driver;
		js.executeScript("arguments[0].scrollIntoView()", ele);
	}
	 
	//封装智能等待方法(设置超时时间:30s)
	private boolean waitToDisplayed(final By loc) {
		boolean waitDisplayed=false;
		waitDisplayed = new WebDriverWait(driver, 30)
				.until(new ExpectedCondition<Boolean>() {
			public Boolean apply(WebDriver d) {
				return d.findElement(loc).isDisplayed();
			}
		});
		return waitDisplayed;
	}
	
	//定位单个元素方法
	public WebElement findEle(By loc){
		WebElement ele = null;
		if(this.waitToDisplayed(loc)){
			ele = driver.findElement(loc);
		}
		return ele;
	}
	
	//定位一组元素方法
	public List<WebElement> findEles(By loc){
		List<WebElement> eles = null;
		if (this.waitToDisplayed(loc)) {
			eles = driver.findElements(loc);
		}
		return eles;
	}

	//判断元素是否出现
	public boolean isElementPresent(By loc){
		boolean isDisplayed = false;
		try{
			isDisplayed = this.findEle(loc).isDisplayed();
		}catch(Exception e){
			isDisplayed = false;
		}
		return isDisplayed;
	}
	
	//点击一个元素
	public void clickEle(By loc){
		WebElement ele = this.findEle(loc);
		ele.click();
	}

	//输入方法
	public void sendKeys(By loc,String value){
		try{
			this.findEle(loc).clear();
			this.findEle(loc).click();
			this.findEle(loc).sendKeys(value);
		}catch(NoSuchElementException e){
			System.err.println("元素"+loc.toString()+"找不到");
		}
	}
	
	// 获取text
	public void getContent(By loc){
		System.out.println("弹窗提示消息:"+ this.findEle(loc).getText());
	}
	
	// alert弹窗
	public void alert(String loc){
		Alert temp=driver.switchTo().alert();
		System.out.println(temp.getText());
		if(loc == "accept"){
			temp.accept();
		}else{
			temp.dismiss();
		}
	}
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值