Anda di halaman 1dari 10

import java.awt.Dimension; import java.awt.Toolkit; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.

InputStream; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.List;

import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult;

import org.apache.commons.io.FileUtils; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle;

import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.util.HSSFColor; import org.openqa.selenium.By; import org.openqa.selenium.NoSuchElementException; import org.openqa.selenium.OutputType; import org.openqa.selenium.TakesScreenshot; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.events.EventFiringWebDriver; import org.w3c.dom.Attr; import org.w3c.dom.Comment; import org.w3c.dom.Document; import org.w3c.dom.Element;

public class LoginWebDriver {

private Xls_Reader readFile(String excelFile){ Xls_Reader data = new Xls_Reader(excelFile); return data; } //Chup man hinh private void captureScreen(WebDriver driver) throws IOException{

//Chp li mn hnh DateFormat dateFormat = new SimpleDateFormat("dd_MM_yyy_HH_mm_ss"); Calendar cal = Calendar.getInstance(); //System.out.println(dateFormat.format(cal.getTime()));

File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

FileUtils.copyFile(scrFile, new File(System.getProperty("user.dir") + "\\screenshots\\" + "LoginWebDriverTest_" + dateFormat.format(cal.getTime()) +".png"));

} //Ghi ket qua test EXCEL private void writeTestResult(String result,int index) throws IOException{ File resFile = new File("C:/Users/Nguyen Viet Anh/workspace/Webdriver_info/src/LoginTest.xls"); FileOutputStream fileOut = new FileOutputStream(resFile,true); HSSFWorkbook workbook = new HSSFWorkbook();

HSSFSheet worksheet = workbook.getSheet("LoginTest"); if(worksheet == null){ worksheet=workbook.createSheet("LoginTest"); } HSSFRow row1 = worksheet.createRow((short) 0); HSSFCell cellA1 = row1.createCell((short) 0); cellA1.setCellValue("Kim th TestLogin"); HSSFCellStyle cellStyle = workbook.createCellStyle();

cellStyle.setFillForegroundColor(HSSFColor.GOLD.index); cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); cellA1.setCellStyle(cellStyle);

HSSFRow row2 = worksheet.createRow((short) index ); HSSFCell cellA2 = row2.createCell((short) 0); cellA2.setCellValue(result);

//Lnh thc hin ghi ra file workbook.write(fileOut);//Ghi vo b nh buffer m thi fileOut.flush();//Ghi ra file fileOut.close(); } //Ghi kt qu XML private void writeToXML(String username, String password, String language){ try {

DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

// root Document doc = docBuilder.newDocument(); Element rootElement = doc.createElement("WebDriverTest"); doc.appendChild(rootElement);

// staff Element staff = doc.createElement("LoginTest"); rootElement.appendChild(staff);

// t thuc tnh Attr attr = doc.createAttribute("name"); attr.setValue("TestLogin"); staff.setAttributeNode(attr); //Comment User Comment commentusername = doc.createComment("Input Username"); staff.appendChild(commentusername); // Username Element user = doc.createElement("username"); user.appendChild(doc.createTextNode(username)); staff.appendChild(user);

// Comment Password Comment commentpass = doc.createComment("Input Password"); staff.appendChild(commentpass); //Password Element pass = doc.createElement("password"); pass.appendChild(doc.createTextNode(password)); staff.appendChild(pass);

Comment commentlang = doc.createComment("Select Language");

staff.appendChild(commentlang); // Language Element lang = doc.createElement("Language"); lang.appendChild(doc.createTextNode(language)); staff.appendChild(lang);

//Vit ni dung vo file XML TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); //Cu trc cy ca XML transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "yes");

DOMSource source = new DOMSource(doc);

//

StreamResult result = new StreamResult(new File("C:\\file.xml"));

transformer.transform(source, result);

//System.out.println("File saved!");

} catch (ParserConfigurationException pce) { pce.printStackTrace();

} catch (TransformerException tfe) { tfe.printStackTrace(); } }

public void Test_UserPassEN() throws IOException, InterruptedException{ WebDriver driver = new FirefoxDriver(); driver.get("http://localhost/manews.vn/administrator/"); //driver.manage().window().maximize();

Xls_Reader data = readFile("C:\\Users\\Nguyen Viet Anh\\workspace\\Webdriver_info\\src\\Data.xlsx"); for(int i=2;i<=data.getRowCount("LoginTest");i++) { String username = data.getCellData("LoginTest", "username", i); String password = data.getCellData("LoginTest", "password", i); String language = data.getCellData("LoginTest", "Language", i);

//Lm chm thi gian ang nhp //Thread.sleep(5000L);

driver.findElement(By.id("modlgn_username")).sendKeys(username); driver.findElement(By.id("modlgn_passwd")).sendKeys(password);

WebElement select = driver.findElement(By.tagName("Select"));

List<WebElement> allOptions = select.findElements(By.tagName("option"));

for(WebElement option: allOptions){ if(option.getText().equals(language)){ option.click(); break; }

} //Thread.sleep(5000L); WebElement element = driver.findElement(By.className("button1")); element.submit();

String results=""; try { String ele=driver.findElement(By.id("modlgn_username")).toString();

if(ele.length()>=0){ results="Li"; //Chp li mn hnh captureScreen(driver); //Ghi kt qu vo Excel writeTestResult(results,i); // Ghi kt qu vo file Xml writeToXML(username,password,language);

continue; }

} catch (NoSuchElementException e) { //ng nhp thnh cng results="ng nhp thnh cng"; //Chp mn hnh captureScreen(driver); //Ghi kt qu vo file Excel writeTestResult(results,i); //Ghi kt qu vo file XML writeToXML(username,password,language);

//Tm phn t Logout

WebElement logoutLink = driver.findElement(By.xpath(".//*[@id='module-status']/span[3]/a")); logoutLink.click();

while(true){ try{ String ele=driver.findElement(By.id("modlgn_username")).toString(); if(ele.length() >=0){ break; }

}catch(NoSuchElementException ex){ }

} continue; }

driver.close(); }

public static void main(String[] args) throws IOException, InterruptedException { LoginWebDriver call_Test = new LoginWebDriver(); call_Test.Test_UserPassEN(); }

Anda mungkin juga menyukai