java+selenium+Firefox自动化测试框架

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_40569991/article/details/80787044

此篇没有太多参考的价值,不够详细.有时间补充


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("609037724@qq.com");
        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("609037724@qq.com");
        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("609037724@qq.com");
        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();


    }
}




展开阅读全文

没有更多推荐了,返回首页