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