您的位置:首页 > 其它

selenium使用学习(一)

2016-03-11 10:14 218 查看
最近在功能测试的基础上,自己打算学习一门自动化测试的工具,根据自己的工作是web端的,所以就定位在学习selenium了,希望可以协同帮助到自己平日的工作。

起初,都是需要什么,直接百度,没有经历录制的阶段,直接开始copy需要的功能的代码,直接开始跑,貌似,最开始的时候是可以这么做的,很高效,因为你最开始用到的都是最简单的,直接用线程的,修改一下url,和你填写的东西,就可以提交了。

这里先copy一段最开始令我兴奋的小代码,希望可以合大家共享,顺便记录自己的学习之路和遇到的问题。

package com.selenium.Glen;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Ref;
import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.IOUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class TestHelloWorld {

private static String userPw;

public static  void main(String[] args) throws FileNotFoundException,
IOException {

System.out.println("start chrome browser...");
String url = "http://user.oneapm.com/pages/v2/login";
String url2 = "http://user.oneapm.com/pages/v2/home";
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(500, TimeUnit.SECONDS);

// System.setProperty("webdriver.chrome.driver",
// "files\\chromedriver.exe");  //指定驱动路径
driver.get(url);
//System.out.println("start chrome browser succeed...");

//读取文件的内容
File file = new File("D:\\mycode\\selenium\\userPwd.txt");
List<String> result = IOUtils.readLines(new FileInputStream(file));
for (String line : result) {
if (line.isEmpty() | line.contains("//")) {
continue;
}

//System.out.println(line);

WebElement element = null;

//登录页面
String emaiBox = "email";//邮箱输入框的name
String passwordBox = "password";//密码输入框的name
String login = "submit";//输入框id

//输入用户名和密码,登录
driver.findElement(By.name(emaiBox)).sendKeys("tihuiping@oneapm.com");
driver.findElement(By.name(passwordBox)).sendKeys("111111a");
driver.findElement(By.name(login)).click();

try {
driver.wait(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

driver.findElement(By.xpath("//span[text()='oneapm的de']")).click();

//selenium.waitForPageToLoad("10000");
driver.getWindowHandle();

driver.findElement(By.xpath("//ul[@id='account-menu']/li[3]/a")).click();
// driver.findElement(By.xpath(".//*[@id='layout']/div[1]/div[2]/div/ul[3]/a")).click();
//ul[@id='account-menu']/li[3]/a
//*[@id="layout"]/div[1]/div[2]/strong/span
//*[@id="layout"]/div[1]/div[2]/div/ul/li[3]/a
//*[@id="layout"]/div[1]/div[2]/strong/span

//	     driver.findElement(By.xpath(".//*[@id='loginform']/div[1]/input")).sendKeys("tihuiping@oneapm.com");
//	     driver.findElement(By.xpath(".//*[@id='loginform']/div[2]/input")).sendKeys("111111a");
//	     driver.findElement(By.xpath(".//*[@id='loginform']/div[4]/input[2]")).click();
//	     driver.findElement(By.xpath(".//*[@id='layout']/div[1]/div[2]/span")).click();
//	     driver.findElement(By.xpath(".//*[@id='layout']/div[1]/div[2]/div/ul[3]/a")).click();

//判断跳转页面是否正确
//	     String currentUrl = driver.getCurrentUrl();
//	     assertTrue("未成功进入待测试页面!",driver.getCurrentUrl().equals(url));

//driver.navigate().to();

//
//	     (new WebDriverWait(driver, 30)).until(new ExpectedCondition<Boolean>() {
//	            public Boolean apply(WebDriver d) {
//	                return d.getTitle().startsWith("OneAPM 应用性能管理云解决方案");
//	            }
//	        });
//

//跳转页面,等待假造完成
//	     //显式等待
//	     WebDriverWait wait = new WebDriverWait(driver,10);
//	        wait.until(new ExpectedCondition<WebElement>(){
//	            public WebElement apply(WebDriver d) {
//	                return d.findElement(By.cssSelector("account-info active"));
//	            }}).click();
//
//	     WebElement element1 = driver.findElement(By.className("account-info active"));
//
//隐式等待
//driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

//	     boolean isElementPresent(By.className("account-info active")) {
//	    	    try {
//	    	      driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
//	    	      driver.findElement(by.className("account-info active"));
//	    	      return true;
//	    	    }
//	    	      catch (NoSuchElementException e) {
//	    	      return false;
//	    	    }
//	    	  }
//	     try {
//			Thread.sleep(3000);
//		} catch (InterruptedException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//	     element1 = driver.findElement(By.className("account"));
//	     System.out.print(element1.getTagName());
//driver.findElement(By.className("account-info active")).click();

//操作过程中时间间隔设置
//	     WebDriverWait wait = (new WebDriverWait(driver,10));
//	     wait.until(new ExpectedCondition<Boolean>(){
//	     public Boolean apply(WebDriver d){
//	     boolean loadcomplete = d.switchTo().frame("right_frame").findElement(By.className("account").isDisplayed();
//	     return loadcomplete;}
//	     });
//thread.sleep(3000);//调试的时候强制延迟
//assertTrue("未成功进入待测试页面!",driver.getTitle().equals(pageTittle));

//	    new WebDriverWait(driver, 15).until(
//	    		    ExpectedConditions.presenceOfElementLocated(By.cssSelector("css locator"))
//	    		);

// driver.close();// 关闭本次执行打开的页面
// driver.quit();//关闭所有页面

}
//return false;

// String title = driver.getTitle();
// String currentUrl = driver.getCurrentUrl();
// String s = driver.getPageSource();
// s = s.substring(s.indexOf("{"), s.indexOf("}"));

// WebElement element = driver.findElement(By.id("passport_user"));
// System.out.println(element.getAttribute("title"));

// System.out.println("the title is " + title);
// System.out.println("the currentUrl is " + currentUrl);
// System.out.println("当前页面的源码:"+s);

}

//	private static void isElementPresent() {
//		// TODO Auto-generated method stub
//
//	}

private static void assertTrue(String string, boolean equals) {
// TODO Auto-generated method stub

}

//	using SIE = OpenQA.Selenium.IE;
//	public static String GoToWindow(String title, Ref SIE.InternetExplorerDriver driver)
//    {
//        driver.SwitchTo().DefaultContent();
//
//        // get all window handles
//        IList<string> handlers = driver.WindowHandles;
//        foreach (var winHandler in handlers)
//        {
//            driver.SwitchTo().Window(winHandler);
//            if (driver.Title == title)
//            {
//                return "Success";
//            }
//            else
//            {
//                driver.SwitchTo().DefaultContent();
//            }
//        }
//
//        return "Window Not Found";
//    }
//
//	private static void assertTrue(String string, boolean equals) {
//		 TODO Auto-generated method stub
//
//	}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: