读取元素

package HelloMaven.HelloMaven;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.Test;


public class Login extends BaseDriver{
	//public static WebDriver driver;

	//public void Initial() {
		//System.setProperty("webdriver.chrome", "F:\\Java\\ChromeDriver\\chromedriver.exe");
		//driver = new ChromeDriver();
		//driver.get("http://www.imooc.com");
		//driver.manage().window().maximize();
	//}
	
	

	public void LoginScript(String username, String password) throws Exception{
		//this.Initial();
		driver.get("http://www.imooc.com");
		Thread.sleep(3000);
		driver.findElement(By.id("js-signin-btn")).click();
		Thread.sleep(3000);

		
		
		WebElement usernameE = this.bystr("username");
		usernameE.isDisplayed();
		WebElement passwordE = this.bystr("password");
		passwordE.isDisplayed();
		usernameE.sendKeys(username);
		passwordE.sendKeys(password);
		WebElement LoginButton = this.bystr("loginbutton");
		LoginButton.isEnabled();

		LoginButton.click();
		Thread.sleep(3000);
		WebElement My_Menu = this.bystr("my_menu");
		Actions action = new Actions(driver);
		action.moveToElement(My_Menu).perform();
		String username_text = this.bystr("nameinfo").getText();
		if(username_text.equals("慕粉3899104")) {
			System.out.println("Login is successfully");
			this.ScreenShot();
		}else {
			System.out.println("Login is failed, unmatched userinfo");
		}
		//driver.quit();
	}
	
	//public WebElement FindEle(By by) {
	//	return driver.findElement(by);
	//}

	public WebElement bystr(String element){
		ProUtil property = new ProUtil("element.properities");
		String location = property.GetProperies(element);
		System.out.println("the file value get from properties file is: " + location);
		String way = location.split(">")[0];
		String value = location.split(">")[1];
		System.out.println("value1 is " + way + ", and value2 is:" + value);
		
		if(way.equals("id")) {
			return driver.findElement(By.id(value));
		}else if(way.equals("name")){
			return driver.findElement(By.name(value));
		}else if(way.equals("className")) {
			return driver.findElement(By.className(value));
		}else if(way.equals("xpath")) {
			return driver.findElement(By.xpath(value));
		}else
			{return null;}
		
	}
	
	public void ScreenShot() {
		long curdate = System.currentTimeMillis();
		String path = String.valueOf(curdate) + ".png";
		String curpath = System.getProperty("user.dir");
		System.out.println(curpath);
		path = curpath + "/" + path;
		File Screen = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
		try {
			FileUtils.copyFile(Screen, new File(path));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@Test
	public void Testsuit() throws Exception {
		//Login as = new Login("Chrome");
		//as.Initial();
		HashMap<String, String> user = new HashMap<String, String>();
		//user.put("sd@s.c", "nedw23eS");
		user.put("13610056184","MISSjj28*");

		Iterator iter = user.entrySet().iterator();
		while (iter.hasNext()) {
			Entry entry = (Entry) iter.next();
			String username = entry.getKey().toString();
			String password = entry.getValue().toString();
			System.out.println("username is "+username+"password is "+password);
			this.LoginScript(username,password);
			Thread.sleep(5000);
			//driver.close();
		}
		//as.LoginScript();
	}

}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值