OneTestCase

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");
  }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值