import java.io.File; import java.util.List; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.firefox.FirefoxProfile; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import File.ExcelWorkBook; public class LoginCenter { private WebDriver driver; private String url; String sourceFile="你文件的路徑和文件名稱"; @BeforeClass public void testBefore(){ //設置firefox瀏覽器 FirefoxProfile file=new FirefoxProfile(new File("C:\\Users\\qinfei\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\t5ourl6s.selenium")); driver=new FirefoxDriver(file); url="你的測試地址"; } @Test public void login() throws Exception{ //初始化ExcelWorkBook Class ExcelWorkBook excelbook=new ExcelWorkBook(); //進入到你的測試界面 driver.get(url); driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS); try{ //把取出的username放在userlist集合里面 List<string> userList=excelbook.readUsername(sourceFile); //把取出的password放在passlist集合里面 List</string><string> passList=excelbook.readPassword(sourceFile); //把取出來的值,輸入到界面的輸入框中 int usersize=userList.size(); for(int i=0;i<usersize ;i++){ //通過css定位到username輸入框 WebElement username=driver.findElement(By.cssSelector("input[name=\"j_username\"]")); //通過css定位到password輸入框 WebElement password=driver.findElement(By.cssSelector("input[name=\"j_password\"]")); //通過xpath定位登錄按鈕 WebElement submit=driver.findElement(By.xpath("http://button//span[contains(text(),'登錄')]")); //清除username輸入框的內容 username.clear(); //把list中數據一個一個的取出來 String name=userList.get(i); //然后填寫到username輸入框 username.sendKeys(name); for(int j=0;j<passList.size();j++){ password.clear(); String pass=passList.get(j); password.sendKeys(pass); } //點擊登錄按鈕 submit.click(); driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS); //通過xpath定位登出按鈕 WebElement logoutButton=driver.findElement(By.xpath("http://button//span[contains(text(),'登出')]")); logoutButton.click(); driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS); } }catch(Exception e){ e.printStackTrace(); } } } |