selenium使用学习(一)

本文介绍了作者开始学习自动化测试工具Selenium的过程,主要用于web端功能测试。通过直接复制并运行简单的代码,作者快速上手,并分享了一段入门级的Selenium代码片段,旨在与读者共享学习心得及可能遇到的问题。

最近在功能测试的基础上,自己打算学习一门自动化测试的工具,根据自己的工作是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
//		
//	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值