执行用java编写的脚本后面临错误(Selenium WEbdriver)

时间:2017-05-16 10:54:40

标签: java selenium-webdriver

我创建了两个文件 我在其中提供浏览器名称和Url的第一个文件 第二个文件,我在其中给出了xpath的逻辑名称。

我已经相应地创建了两个班级 在第一节课中,我创造了所有的功能 在第二个类中,iam调用第一个类中的函数(使用继承概念[extends])。

上传所有文件。
第一档:

Browser=Firefox
AppURL=https://www.myntra.com/

第二档:

Myntra_Search_Xpath=.//*[@id='desktop-header-cnt']/div/div[3]/input

头等舱

package Basepack;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

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

public class baseclass {
    public static FirefoxDriver driver;

    public static String  getpropValue(String PName) throws IOException{
        Properties prop=new Properties();
        FileInputStream pi=new FileInputStream("C:\\Users\\Desktop\\workspace\\SeleFirstClass\\src\\Basepack\\Objprop");
        prop.load(pi);
        String Pvalue=prop.getProperty(PName);
        return Pvalue;

    }

    public static String getpathValue(String Pname) throws IOException{
        Properties prop=new Properties();
        FileInputStream pi=new FileInputStream("C:\\Users\\Desktop\\workspace\\SeleFirstClass\\src\\Basepack\\Objpath");
        prop.load(pi);
        String Pvalue=prop.getProperty(Pname);
        return Pvalue;

    }
    public static void openBrowser(){
        System.setProperty("webdriver.gecko.driver", "D:\\Selenium\\geckodriver.exe");
        driver=new FirefoxDriver();

    }
    public static void navigate(String URLName) throws IOException{
     String path=getpropValue(URLName);
     driver.get(path);
    }
    public static void closeBrowser(){
        driver.close();
    }
    public static void click(String Value) throws IOException{
        String path=getpathValue(Value);
        driver.findElement(By.xpath(path)).click();
    }

    public static void type(String path, String Value) throws IOException{
        String Locator=getpathValue(path);
        driver.findElement(By.xpath(Locator)).sendKeys(Value);
    }

public static void wait(int i) throws InterruptedException
   {
      Thread.sleep(i*1000);
   }
}

第二课

package Basepack;

import java.io.IOException;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class FirstTestNG extends baseclass{
    @BeforeMethod
    public void beforemethod() throws IOException, InterruptedException
    {
        openBrowser();
        navigate("AppURL");
        wait(3000);

    }
    @AfterMethod
    public void aftermethod()
    {
        closeBrowser();
    }

    @Test
    public void invalidlogin1() throws IOException, InterruptedException
    {
        wait(3000);
        type("Flipkart_login_Xpath", "Nike Shoes");
    }

}

以下是错误说明

1494929368178   geckodriver INFO    Listening on 127.0.0.1:8576
1494929368783   geckodriver::marionette INFO    Starting browser \\?\C:\Program Files\Mozilla Firefox\firefox.exe with args ["-marionette"]
1494929369821   addons.manager  ERROR   startup failed: [Exception... "Component returned failure code: 0x80070057 (NS_ERROR_ILLEGAL_VALUE) [nsIFile.create]"  nsresult: "0x80070057 (NS_ERROR_ILLEGAL_VALUE)"  location: "JS frame :: resource://gre/modules/FileUtils.jsm :: FileUtils_getDir :: line 70"  data: no] Stack trace: FileUtils_getDir()@resource://gre/modules/FileUtils.jsm:70 < FileUtils_getFile()@resource://gre/modules/FileUtils.jsm:42 < AddonManagerInternal.validateBlocklist()@resource://gre/modules/AddonManager.jsJavaScmr:i6p6t5  e<r rAodrd:o nrMeasnoaugrecreI:n/t/egrrnea/lm.osdtualretsu/pA(d)d@orneMsaonuargceer:./j/sgmr,e /lmionde 1639: NS_ERROR_NOT_INITIALIZED: AddonManager is not initialized
ules/AddonManager.jsm:832 < this.AddonManagerPrivate.startup()@resource://gre/modules/AddonManager.jsm:2773 < amManager.prototype.observe()@resource://gre/components/addonManager.js:57
1494929371707   Marionette  INFO    Listening on port 53003
JavaScript error: undefined, line 492: Component returned failure code: 0x8000ffff (NS_ERROR_UNEXPECTED) [nsIPrefBranch.getCharPref]
JavaScript error: resource://gre/modules/AddonManager.jsm, line 2484: NS_ERROR_NOT_INITIALIZED: AddonManager is not initialized
May 16, 2017 3:39:33 PM org.openqa.selenium.remote.ProtocolHandshake createSession`enter code here`INFO: Detected dialect: W3C

1 个答案:

答案 0 :(得分:0)

尝试添加DesiredCapabilities

   DesiredCapabilities capabilities = DesiredCapabilities.firefox();
   capabilities.setCapability("marionette", true);
   WebDriver driver = new FirefoxDriver(capabilities);