package com.mushishi.Imooc;
import org.testng.annotations.Test;
import com.google.common.io.Files;
import com.mushishi.Imooc.util.ProUtil;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Listeners;
import org.testng.annotations.AfterTest;
@Listeners({TestNGListenerScreen.class})
public class OneTestCase {
public WebDriver driver;
@Test
public void f() {
ProUtil pro = new ProUtil("user.properties");
String user = null;
String username;
String password;
int Lines = pro.GetLines();
for(int i = 0;i<Lines;i++){
// InitDriver();
user = pro.GetPro("user"+i); //username=name>email
username = user.split(">")[0];
password = user.split(">")[1];
//String UserName = "mushishi_xu@163.com";
//String PassWord = "xu221168";
// WebElement EmailElement = driver.findElement(By.name(UserElement));
// WebElement EmailElement = driver.findElement(this.GetByLocal(UserGetBy, UserElement));
WebElement EmailElement = GetElement("username");
WebElement PassWordElement = GetElement("password");
WebElement LoginButtonElement = GetElement("loginbutton");
EmailElement.sendKeys(username);
PassWordElement.sendKeys(password);
LoginButtonElement.click();
//TakeScreenShot();
System.out.println("001");
try {
Thread.sleep(3000);
System.out.println("sleep");
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println("002");
try {
System.out.println("003");
WebElement UserPng = GetElement("headpng");
System.out.println("004");
// 鼠标事件
System.out.println("005");
Actions MoseActions = new Actions(driver);
System.out.println("006");
MoseActions.moveToElement(UserPng).perform();
System.out.println("007");
String UserNameInfo = GetElement("userinfo").getText();
if(UserNameInfo.equals("mushishi_xu")) {
System.out.println("登录成功");
}else {
System.out.println("用户信息不匹配"+UserNameInfo);
}
}catch(Exception e) {
System.out.println("登录失败");
}
EmailElement.clear();
PassWordElement.clear();
}
}
@Test
public void f1() {
System.out.print("This is test case f1");
}
@BeforeClass
public void beforeClass() {
driver = new ChromeDriver();
driver.get("https://www.imooc.com/user/newlogin");
}
public By GetByLocal(String key) {
ProUtil pro = new ProUtil("element.properties");
String Locator = pro.GetPro(key); //username=name>email
String LocatorBy = Locator.split(">")[0];
String LocatorValue = Locator.split(">")[1];
if(LocatorBy.equals("id")) {
System.out.println("010");
return By.id(LocatorValue);
}else if(LocatorBy.equals("name")) {
return By.name(LocatorValue);
}else if(LocatorBy.equals("className")) {
return By.className(LocatorValue);
}else {
return By.xpath(LocatorValue);
}
}
/* 封装一个方法:读取配置文件的properties
*
*/
public void GetByLocater(String key) {
}
/* 封装 element 方法
*
*/
public WebElement GetElement(String key) {
WebElement Element = driver.findElement(this.GetByLocal(key));
return Element;
}
public void TakeScreenShot(){
//图片名字
//图片存放的路径
//获取当前时间
SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
String curTime = sdf.format(new Date());
//获取当前类名
String curClassName = this.getClass().getName();
String pngPath = curClassName+"_"+curTime+".png";
//路径
String curPath = System.getProperty("user.dir");
File ScrFile = ((RemoteWebDriver) driver).getScreenshotAs(OutputType.FILE);
try {
Files.copy(ScrFile,new File(curPath+"\\"+pngPath));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@AfterClass
public void afterClass() {
driver.close();
}
@BeforeMethod
public void beForeMethod(){
System.out.print("This is test beForeMethod");
}
@AfterMethod
public void afterMethod(){
System.out.print("This is test afterMethod");
}
@BeforeTest
public void beforeTest() {
System.out.print("This is test beforeTest");
}
@AfterTest
public void afterTest() {
System.out.print("This is test afterTest");
}
}
OneTestCase
最新推荐文章于 2021-10-17 22:15:56 发布