无法使用selenium脚本在mozilla firefox中写入URL。甚至eclipse都没有显示任何错误

时间:2017-07-14 07:19:42

标签: java selenium testing mozilla

尝试通过selenium脚本测试ikea主页.Mozilla fire fox已打开,但地址栏中输入了url。

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

public class ikeaautomation {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        // declaration and instantiation of objects/variables
        WebDriver driver ;
        System.setProperty("webdriver.firefox.marionette","C:\\Users\\orange\\Downloads\\geckodriver.exe");
        driver = new FirefoxDriver();
        String baseUrl = "http://ikea.in";
        String expectedTitle = "IKEA";
        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();

        // exit the program explicitly
        System.exit(0);

    }

}

1 个答案:

答案 0 :(得分:1)

以下是您的问题的答案:

当你使用Selenium 3.4.0,geckodriver v0.17.0,Mozilla 53.0到Selenium-Java绑定,而不是 webdriver.firefox.marionette 时,你必须提到 {{1} } webdriver.gecko.driver,如下所示:

System.setProperty

让代码正常工作:

修改后的代码:

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

控制台输出:

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

    // get the actual value of the title
    actualTitle = driver.getTitle();
    System.out.println("Actual : "+actualTitle);
    System.out.println("Expect : "+expectedTitle);

    /*
     * 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");
    }

如果能回答你的问题,请告诉我。