用Selenium自动化启动3大浏览器的方法

1\如果复制JAVA文件到eclipse工程内出现乱码,则右击工程,选择属性--resource -text file encoding- other:UTF-8

2\导入现有JAR包:右击工程,选择build path--java build path-add external jars


一、Java中System.setProperty()用法

/*
 * 设置指定键对值的系统属性
 * setProperty (String prop, String value);
 * 
 * 参数:
 * prop - 系统属性的名称。
 * value - 系统属性的值。  
 * 
 * 返回:
 * 系统属性以前的值,如果没有以前的值,则返回 null。
 * 
 * 抛出:  
 * SecurityException - 如果安全管理器存在并且其 checkPermission 方法不允许设置指定属性。
 * NullPointerException - 如果 key 或 value 为 null。
 * IllegalArgumentException - 如果 key 为空。
 * 注:这里的system,系统指的是 JRE (runtime)system,不是指 OS。
 * 
*/

//实例
System.setProperty("Property1", "abc");
System.setProperty("Property2","def");

//这样就把第一个参数设置成为系统的全局变量!可以在项目的任何一个地方 通过System.getProperty("变量");来获得,

//System.setProperty 相当于一个静态变量  ,存在内存里面!

复制代码
public class SystemTest {
    
    static {
        setValue();
    }

    public static void setValue() {
        System.setProperty("name", "张三");
        System.setProperty("age", "28");
    }
    
    public static void main(String[] args) {
        System.out.println(System.getProperty("name"));
        System.out.println(System.getProperty("age"));
    }
}


2、

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.support.ui.ExpectedCondition;  
import org.openqa.selenium.support.ui.WebDriverWait;  
  
public class Selenium2Example  {  
    public static void main(String[] args) {  
        // Create a new instance of the Firefox driver  
        // Notice that the remainder of the code relies on the interface,   
        // not the implementation.  
        WebDriver driver = new FirefoxDriver();  
  
        // And now use this to visit Google  
        driver.get("http://www.google.com");  
        // Alternatively the same thing can be done like this  
        // driver.navigate().to("http://www.google.com");  
  
        // Find the text input element by its name  
        WebElement element = driver.findElement(By.name("q"));  
  
        // Enter something to search for  
        element.sendKeys("Cheese!");  
  
        // Now submit the form. WebDriver will find the form for us from the element  
        element.submit();  
  
        // Check the title of the page  
        System.out.println("Page title is: " + driver.getTitle());  
          
        // Google's search is rendered dynamically with JavaScript.  
        // Wait for the page to load, timeout after 10 seconds  
        (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {  
            public Boolean apply(WebDriver d) {  
                return d.getTitle().toLowerCase().startsWith("cheese!");  
            }  
        });  
  
        // Should see: "cheese! - Google Search"  
        System.out.println("Page title is: " + driver.getTitle());  
          
        //Close the browser  
        driver.quit();  
    }  
}  


3、
//以下是启动IE浏览器脚本(先要下载IE驱动,并指定路径,方法如下)--调试
		/*System.setProperty("webdriver.ie.driver", "D:/selenium自动化脚本/iedriver/IEDriverServer.exe");
		
		DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer();
		ieCapabilities.setCapability	(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);
		WebDriver driver = new InternetExplorerDriver(ieCapabilities);
		driver.get("http://soasadmin-stg.paic.com.cn/admin/admin/login.html");
		*/


//以下是启动火弧浏览器脚本--调试成功,也需要安装驳运才可以,已下载火弧驱动
 System.setProperty("webdriver.gecko.driver","D:/myselenium/geckodriver.exe");// --指定安装目录就要指定浏览器路径

		WebDriver driver = new FirefoxDriver();// 默认安装路径就不需要指定以上语句,直接用下面2句启动
		driver.get("http://www.baidu.com");
		//WebElement d=driver.findElement(By.name("百度一下"));
		//d.click();
		
		
		driver.findElement(By.name("wd")).sendKeys("我爱中国人");
		driver.findElement(By.className("bg s_btn")).click();

		
//以下是启动谷歌浏览器脚本--没调试成功
	System.setProperty("webdriver.chrome.driver", "C:/Program Files (x86)/Google/Chrome/Application/chromedriver.exe");  
		WebDriver driver = new ChromeDriver();
		driver.get("http://www.baidu.com")

 

其他代码:供参考

1、LoginAdmin

package webselenium.login;

import java.io.FileNotFoundException;
import java.io.IOException;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import webselenium.common.Common;

public class LoginAdmin{
	
	public static void loginAdmin(WebDriver driver) throws FileNotFoundException, IOException{
		driver.get(Common.getPropertiesFromKeyToValue("at"));//Fat或Uat切换
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		driver.findElement(By.className(Common.getPropertiesFromKeyToValue("elementUserName"))).sendKeys(Common.getPropertiesFromKeyToValue("username"));
		driver.findElement(By.className(Common.getPropertiesFromKeyToValue("elementPassWord"))).sendKeys(Common.getPropertiesFromKeyToValue("password"));
		WebElement cmd = driver.findElement(By.xpath(("/html/body/div/div[3]/div[3]/a")));
		JavascriptExecutor js = (JavascriptExecutor)driver;  
		js.executeScript("arguments[0].click();", cmd);
		
	}
}

2、

import org.openqa.selenium.WebDriver;

public class CloseBroser{
    public static void close(WebDriver driver){
        //关闭浏览器
        driver.quit();
    }
}

 

转载于:https://my.oschina.net/u/3563297/blog/1557152

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值