由于驱动程序无效,无法启动Chrome浏览器

时间:2018-03-01 08:37:50

标签: selenium selenium-webdriver selenium-chromedriver junit4

我正在使用shell脚本文件执行我的代码,并且根据要求,如果我不传递参数,那么我的代码将在我的本地机器上执行,如果我将使用shell脚本传递参数,那么代码应该在浏览器堆栈上执行。如果我正在使用参数执行,那么我的代码工作正常。但是,如果我没有在shell脚本中传递参数,则无法启动chrome浏览器,因为在If条件驱动程序为null时。这是我的基类

当我执行没有参数的shell脚本时,我的执行将进入If条件但无法启动chrome浏览器,因为如果条件没有初始化驱动程序变量。如果我正在打印驱动程序值,那么它显示为空。

package com.epath.smoketest.tests;
/** 
 * Class: Base 
 * Author: D Albanese 
 * Creation Date: 4/5/2017 
 */

import org.junit.Rule;
import org.junit.rules.ExternalResource;
import org.openqa.selenium.WebDriver;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.LocalFileDetector;
import org.openqa.selenium.remote.RemoteWebDriver;

public class Base {
    //-Browser capability passed by CLI argument.
    private static String sBrowser;
    //-Browser version passed by CLI argument.
    private static String sBversion;
    //-OS capability passed by CLI argument.
    private static String sOsName;
    //-OS version capability passed by CLI argument.
    private static String sOsVersion;
    //-Passing input folder name by CLI argument.       
    public String sFolderName = "resources";
    public static String getExecutionPath;
    public static String getResourcePath;
    public static WebDriver driver;

    public Base(String sBrowser, String sBversion, String sOsName, String sOsVersion,String sFolderName) {
        this.sBrowser = sBrowser;
        this.sBversion = sBversion;
        this.sOsName = sOsName;
        this.sOsVersion = sOsVersion;
        if(null != sFolderName && ! sFolderName.trim().equals("")) {
            this.sFolderName = sFolderName;
        }
    }

    //-Utilizing ExternalResource rule to preserve functionality of @Before and @After annotations in tests
    //-ExternalResource rule has before and after methods that execute prior to methods annotated with @Before and @After
    @Rule
    public ExternalResource resource = new ExternalResource() {

        @Override
        protected void before() throws Throwable {
            //-Use this for local testing

            DesiredCapabilities caps = new DesiredCapabilities();
            caps.setCapability("browser", sBrowser);
            caps.setCapability("browser_version", sBversion);
            caps.setCapability("os", sOsName);
            caps.setCapability("os_version", sOsVersion);
            caps.setCapability("folder_name", sFolderName);
            caps.setCapability("browserstack.local", "true");

            if(sBrowser.length() == 0 && sBversion.length() == 0 && sOsName.length() == 0 && sOsVersion.length() == 0)
            {           
                System.out.println("Inside If Condition ");
                //-Load the properties
                Properties prop = new Properties();
                InputStream input = null;
                input = new FileInputStream(System.getProperty("user.home") +
                        "/epath/services/tests/resources/AutomationData.properties");
                prop.load(input);
                //  getResourcePath=prop.getProperty("resources_path");

                getExecutionPath = prop.getProperty("local_resources_path");
                System.out.println("Print Execution Path :- " +getExecutionPath);
                System.out.println("Print Driver Path :- " + driver);
                System.setProperty("webdriver.chrome", "\\\\192.168.10.21\\volume1\\ngage_dev\\engineering\\ngage\testing\\automated\\chromedriver_win32\\chromedriver.exe");
                driver = new ChromeDriver();

            } else {
                //-Load the properties
                System.out.println("Inside else Condtions ");
                Properties prop = new Properties();
                InputStream input = null;
                input = new FileInputStream(System.getProperty("user.home") +
                        "/epath/services/tests/resources/AutomationData.properties");
                prop.load(input);
                getResourcePath=prop.getProperty("resources_path");
                getExecutionPath = prop.getProperty("resources_path")+sFolderName;  

                //-Get USERNAME and AUTOMATE_KEY of browser stack
                String browserStackUsername = prop.getProperty("browser_stack_username");
                String browserStackAutomateKey = prop.getProperty("browser_stack_automate_key");
                String URL = "https://" + browserStackUsername + ":" +
                        browserStackAutomateKey + "@hub-cloud.browserstack.com/wd/hub";
                driver = new RemoteWebDriver(new URL(URL), caps);
                //-Load the URL to be tested
                driver.get(prop.getProperty("test_url"));
                //-For local file uploads 
                ((RemoteWebDriver) driver).setFileDetector(new LocalFileDetector());

            }
        }

        @Override
        protected void after() {
            driver.quit();
        }
    };

    public String getAutomationInputDataPath() {
        return this.getExecutionPath;
    }

    public static String getResourcePathFromPropertiesfile() {
        return getResourcePath;
    }
}

这是我的测试用例类,我在这里调用基类(上面的类)

public class AddRegisterLAs extends Base {

    private Login login;
    private Navigation go;
    private LearningActivityAdd addLa;
    private ImageAdd addImage;
    private DocumentAdd addDocument;
    private VideoAdd addVideo;
    private AudioAdd addAudio;
    private LinkAdd addLink;
    private CustomAdd addCustom;
    private AiccAdd addAicc;
    private ScormAdd addScorm;
    private RegistrationCreate createRegistration;
    private Utils utils;
    private GetVersion getVersion;

    public AddRegisterLAs() {       
        super(System.getProperty("browser"),System.getProperty("browser_version"),System.getProperty("os"),System.getProperty("ov"),System.getProperty("folderName"));      
    }

    @Before
    public void setUp() {

        login = new Login(driver);
        go = new Navigation(driver);        
        addLa = new LearningActivityAdd(driver);
        addImage = new ImageAdd(driver);
        addDocument = new DocumentAdd(driver);
        addVideo = new VideoAdd(driver);
        addAudio = new AudioAdd(driver);
        addLink = new LinkAdd(driver);
        addCustom = new CustomAdd(driver);
        addAicc = new AiccAdd(driver); 
        addScorm = new ScormAdd(driver);
        utils = new Utils();
        getVersion = new GetVersion(driver);
        createRegistration = new RegistrationCreate(driver);
    }

    @Test
    public void Shallow() throws Exception {

        //utils.logAndPrint("AddRegisterLAs");
        int maxLAs = 1000;
        int maxRegs = 1000;

        //-Print to screen to create log.  Log can be copied and pasted to Word document or elsewhere as needed.
        System.out.println("\n" + "---------------------------------------------------------------------------------"+ "\r\n");
        System.out.println("---------------------------------------------------------------------------------"+ "\r\n");
        System.out.println("Adding and Registering Learning Activities Automation"+ "\r\n");
        System.out.println("---------------------------------------------------------------------------------"+ "\r\n");
        System.out.println("---------------------------------------------------------------------------------"+ "\r\n");

        //-Load the properties
        System.out.println("\n" + "---------------------------------------------------------------------------------"+ "\r\n");
        System.out.println("Read in PROPERTIES file"+ "\r\n");
        System.out.println("---------------------------------------------------------------------------------"+ "\r\n");

        Properties prop = new Properties();
        InputStream input = null;

1 个答案:

答案 0 :(得分:0)

首先请检查您是否可以访问您在里面提到的驱动程序路径。