Selenium实现UI自动化案例

1. 登陆doclever(http://www.doclever.cn/controller/index/index.html(自己注册)

2. 进行个人资料设置(头像、年龄、性别、公司、qq、邮箱、手机)

3. 退出登陆

代码如下:

package com.my.finalcase;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import org.sikuli.script.FindFailed;
import org.sikuli.script.Pattern;
import org.sikuli.script.Screen;

public class AutoTestTool {

	public static String readProperties(String p_key) {

		String value = null;
		FileInputStream stream = null;
		String file = System.getProperty("user.dir") + File.separator + "config.properties";

		try {
			stream = new FileInputStream(file);
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		}

		Properties prop = new Properties();

		try {
			prop.load(stream);
			value = prop.getProperty(p_key);
			stream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return value;
	}

	public static boolean isPicExist(String p_iconPath) {

		Pattern pattern = new Pattern(p_iconPath);
		Screen screen = new Screen();
		try {
			screen.find(pattern).getImage();
			return true;

		} catch (FindFailed e) {
			return false;
		}

	}

	public static void mySleep(long p_time) {
		try {
			Thread.sleep(p_time * 1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

	}
}
package com.my.finalcase;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

public class BusinessLib {
	
    public static void login(WebDriver driver, String p_user, String p_pwd) {
    	driver.get("http://www.doclever.cn/controller/index/index.html");
    	driver.findElement(By.xpath("//div[@id='app']/div/div")).click();
    	driver.findElement(ObjectStore.loginLink).click();
    	driver.findElement(ObjectStore.userName).click();
    	driver.findElement(ObjectStore.userName).clear();
    	driver.findElement(ObjectStore.userName).sendKeys(p_user);
    	driver.findElement(ObjectStore.password).click();
    	driver.findElement(ObjectStore.password).clear();
    	driver.findElement(ObjectStore.password).sendKeys(p_pwd);
    	driver.findElement(ObjectStore.loginButton).click();
    	try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
    	
    }
}
package com.my.finalcase;

import org.openqa.selenium.By;

public class ObjectStore {
	
	public static final By loginLink = By.linkText("登录");
	public static final By userName = By.xpath("//input[@type='text']");
	public static final By password = By.xpath("//input[@type='password']");
	public static final By loginButton = By.id("login");
	public static final By settingIcon = By.xpath("//div[@id='app']/div[2]/div/div[3]");
	public static final By userIcon = By.xpath("//div[@id='app']/div/div[2]/div/div[3]/div/span/span");

}
package com.my.webdriver;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class MyCase {

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}

	@Before
	public void setUp() throws Exception {
	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void case1_login() {
		System.out.println("login");                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
	}
	
	@Test
	public void case2_setting() {
		System.out.println("setting");
	}
	
	@Test
	public void case3_logout() {
		System.out.println("logout");
	}

}
package com.my.webdriver;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import com.my.finalcase.AutoTestTool;
import com.my.finalcase.BusinessLib;
import com.my.finalcase.ObjectStore;

public class UntitledTestCase {
	private WebDriver driver;
	private String baseUrl;
	private boolean acceptNextAlert = true;
	private StringBuffer verificationErrors = new StringBuffer();

	@Before
	public void setUp() throws Exception {
		driver = new ChromeDriver();
		baseUrl = "https://www.google.com/";
		// driver.get(AutoTestTool.readProperties("url"));
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
	}

	@Test
	public void testUntitledTestCase() throws Exception {
		driver.manage().window().maximize();
		BusinessLib.login(driver, "jk159820", "jK@159820");
		assertTrue(isElementPresent(ObjectStore.userIcon));
		driver.findElement(ObjectStore.settingIcon).click();
		Thread.sleep(1000);
		driver.findElement(By.id("file")).sendKeys("E:\\头像.jpg");

		driver.findElement(By.name("age")).click();
		driver.findElement(By.name("age")).clear();
		driver.findElement(By.name("age")).sendKeys("31");
		driver.findElement(By.name("sex")).click();
		Thread.sleep(1000);
		driver.findElement(By.xpath("//div[2]/div/div/ul/li[2]")).click();
		driver.findElement(By.name("company")).click();
		driver.findElement(By.name("company")).clear();
		driver.findElement(By.name("company")).sendKeys("易宝软件");
		driver.findElement(By.name("qq")).click();
		driver.findElement(By.name("qq")).clear();
		driver.findElement(By.name("qq")).sendKeys("421045215");
		driver.findElement(By.name("email")).click();
		driver.findElement(By.name("email")).clear();
		driver.findElement(By.name("email")).sendKeys("421045215@qq.com");
		driver.findElement(By.name("phone")).click();
		driver.findElement(By.name("phone")).clear();
		driver.findElement(By.name("phone")).sendKeys("15982092000");
		driver.findElement(By.xpath("//div[@id='personInfo']/div/div[3]/form/div[4]/div/button/span")).click();
		Thread.sleep(2000);

		String expectedList[] = { "31", "女", "易宝软件", "421045215", "421045215@qq.com", "15982092000" };
		String actualList[] = { driver.findElement(By.name("age")).getAttribute("value"),
				driver.findElement(By.name("sex")).getAttribute("value"),
				driver.findElement(By.name("company")).getAttribute("value"),
				driver.findElement(By.name("qq")).getAttribute("value"),
				driver.findElement(By.name("email")).getAttribute("value"),
				driver.findElement(By.name("phone")).getAttribute("value"), };
		assertArrayEquals(expectedList, actualList);

		driver.findElement(By.xpath("//div[@id='app']/div/div[2]/div/div[3]/div/span/span")).click();
		Thread.sleep(1000);
		driver.findElement(By.xpath("//ul[starts-with(@id,'dropdown-menu-')]/li[2]")).click();
		Thread.sleep(1000);
		assertTrue(isElementPresent(By.linkText("登录")));
	}

	@After
	public void tearDown() throws Exception {
		driver.quit();
		String verificationErrorString = verificationErrors.toString();
		if (!"".equals(verificationErrorString)) {
			fail(verificationErrorString);
		}
	}

	private boolean isElementPresent(By by) {
		try {
			driver.findElement(by);
			return true;
		} catch (NoSuchElementException e) {
			return false;
		}
	}

	private boolean isAlertPresent() {
		try {
			driver.switchTo().alert();
			return true;
		} catch (NoAlertPresentException e) {
			return false;
		}
	}

	private String closeAlertAndGetItsText() {
		try {
			Alert alert = driver.switchTo().alert();
			String alertText = alert.getText();
			if (acceptNextAlert) {
				alert.accept();
			} else {
				alert.dismiss();
			}
			return alertText;
		} finally {
			acceptNextAlert = true;
		}
	}
}

config.properties配置文件中内内容如下:

url=https://www.google.com/
user=jk159820
pwd=jK@159820

运行结果如下: 

公司大股东

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值