在java代码执行中发出代码

时间:2017-08-22 12:16:33

标签: selenium

要开始使用,您需要导入以下两个包:

org.openqa.selenium。* - 包含实例化一个加载了特定驱动程序的新浏览器所需的WebDriver类 org.openqa.selenium.firefox.FirefoxDriver - 包含将特定于Firefox的驱动程序实例化到WebDriver类实例化的浏览器所需的FirefoxDriver类 如果您的测试需要更复杂的操作,例如访问另一个类,浏览器截图或操作外部文件,那么您肯定需要导入更多的包。 无法理解代码??

   package newproject;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
//comment the above line and uncomment below line to use Chrome
//import org.openqa.selenium.chrome.ChromeDriver;
public class PG1 {


    public static void main(String[] args) {
        // declaration and instantiation of objects/variables
        System.setProperty("webdriver.firefox.marionette","C:\\geckodriver.exe");
        WebDriver driver = new FirefoxDriver();
        //comment the above 2 lines and uncomment below 2 lines to use Chrome
        //System.setProperty("webdriver.chrome.driver","G:\\chromedriver.exe");
        //WebDriver driver = new ChromeDriver();

        String baseUrl = "http://demo.guru99.com/selenium/newtours/";
        String expectedTitle = "Welcome: Mercury Tours";
        String actualTitle = "";

        // launch Fire fox and direct it to the Base URL
        driver.get(baseUrl);

        // get the actual value of the title
        actualTitle = driver.getTitle();

        /*
         * compare the actual title of the page with the expected one and print
         * the result as "Passed" or "Failed"
         */
        if (actualTitle.contentEquals(expectedTitle)){
            System.out.println("Test Passed!");
        } else {
            System.out.println("Test Failed");
        }

        //close Fire fox
        driver.close();

    }

}

1 个答案:

答案 0 :(得分:2)

您的代码看起来很好。你只需要改变:

这段代码:

System.setProperty("webdriver.firefox.marionette","C:\\geckodriver.exe");

到这一行:

System.setProperty("webdriver.gecko.driver","C:\\geckodriver.exe");