1. 程式人生 > >java+selenium+Firefox自動化測試框架

java+selenium+Firefox自動化測試框架

此篇沒有太多參考的價值,不夠詳細.有時間補充

FireFox下載28-32版本的,我下載的是,較高版本不支援FireBug和FirePath

新增元件


搜尋安裝FireBug


安裝FirePath外掛


下載一下紅框內的包

其中selenium-server-3.1.0包內包含的檔案

將第三方jar包(lib和)全部新增到專案中.

建立專案的類

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class demo {
    public static void main(String[] args) {
        WebDriver driver;
        //火狐的安裝位置
        System.setProperty("webdriver.firefox.bin","D:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
        //載入驅動
        System.setProperty("webdriver.firefox.marionette","D:\\jar\\seleniumforFirefox\\geckodriver-v0.14.0-win64\\geckodriver.exe");
        driver=new FirefoxDriver();
        driver.get("https://www.baidu.com");
    }
}

另一個例子:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class ActionSelenium {
    public WebDriver driver;

    public void InitDriver() {
        System.setProperty("webdriver.firefox.bin", "D:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
        //System.setProperty("webdriver.chrome.bin","C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe");
       // System.setProperty("webdriver.chrome.driver", "D:\\jar\\seleniumjar\\chromedriver_win32\\chromedriver.exe");
        System.setProperty("webdriver.firefox.marionette","D:\\jar\\seleniumforFirefox\\geckodriver-v0.14.0-win64\\geckodriver.exe");
        //driver = new ChromeDriver();
        driver=new FirefoxDriver();
        driver.get("https://www.imooc.com/user/newlogin/from_url");
        driver.manage().window().maximize();
    }
    public void inputBox(){
        driver.findElement(By.name("email")).sendKeys("
[email protected]
"); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } driver.findElement(By.name("email")).clear(); String s=driver.findElement(By.name("email")).getAttribute("placeholder"); System.out.print(s); } public static void main(String[] args) { ActionSelenium as=new ActionSelenium(); as.InitDriver(); as.inputBox(); } }

下一個例子

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

import java.util.Collections;
import java.util.List;

public class selenium1 {
    public static void main(String[] args) {
        System.setProperty("webdriver.firefox.bin", "D:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
        System.setProperty("webdriver.firefox.marionette","D:\\jar\\seleniumforFirefox\\geckodriver-v0.14.0-win64\\geckodriver.exe");
        WebDriver driver = new FirefoxDriver();
        driver.get("http://www.imooc.com/user/newlogin/form_url");
        driver.findElement(By.tagName("input")).sendKeys("[email protected]");
        driver.findElement(By.name("password")).sendKeys("xu221168");
        driver.findElement(By.id("auto-signin")).click();
        driver.findElement(By.linkText("立即註冊")).click();
        driver.findElement(By.partialLinkText("直接")).click();
        //driver.findElement(By.tagName("input")).sendKeys("[email protected]");
        driver.findElement(By.name("password")).sendKeys("xu221168");
        //driver.findElement(By.className("btn-red")).click();
        driver.manage().window().maximize();
        driver.get("https://www.imooc.com/");
        driver.findElement(By.className("search-input")).click();
        driver.findElement(By.className("search-input")).sendKeys("ssss");
        WebElement element = driver.findElement(By.className("nav-item"));
        List<WebElement> elements = Collections.singletonList(element.findElement(By.tagName("li")));
        elements.get(3).click();


    }
}