tupac shakur
tupac shakur

Reputation: 678

How to run selenium webdriver from Linux (currently working in windows)?

I have this code which is running locally using my IDE (intellij):

public class ConnectAndBrowse {
    WebDriver driver;
    private String m_baseUrl = "https://tinyurl.com/";
    private String m_toShortenURL;
    private ArrayList<String> tabs2;

    public ConnectAndBrowse( String i_toShortenURL ) throws MalformedURLException {
        setUp(i_toShortenURL);
    }

    private void setUp(String i_toShortenURL) throws MalformedURLException {
        System.setProperty("webdriver.chrome.driver","./src/main/resources/drivers/chromedriver.exe");
        driver = new ChromeDriver();
        m_toShortenURL = i_toShortenURL;
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    }

    public WebDriver browseToUrlWithShortLink() throws Exception {
        driver.get(m_baseUrl);
        driver.findElement(By.id("url")).click();
        driver.findElement(By.id("url")).clear();
        driver.findElement(By.id("url")).sendKeys(m_toShortenURL);
        driver.findElement(By.id("submit")).click();
        driver.findElement(By.linkText("Open in new window")).click();
        return driver;
    }

    public String returnShortLink(WebDriver driver) {

        String data = driver.findElement(By.xpath("//*[@id=\"contentcontainer\"]/div[2]/b")).getText();
        return data;
    }

}

and this is my main class:

public class ManagerService {

    public static void main(String[] args) {
        try {
            FactoryHelper factoryHelper = new FactoryHelper();
            Properties prop = factoryHelper.getPropFile();
            String toShorten = prop.getProperty("defaultUrl");
            ConnectAndBrowse connectAndBrowse = new ConnectAndBrowse(toShorten);
            WebDriver driver=connectAndBrowse.browseToUrlWithShortLink();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

I am using maven (pom.xml) just to download dedicated drivers. Now, I want to run it from Linux and I am struggling doing it. any idea what I suppose to add to my code ?

Upvotes: 2

Views: 10366

Answers (1)

Navarasu
Navarasu

Reputation: 8479

Your driver version has to be changed according the linux version. You can download linux chromedriver version and put it in resource folder. You can append .exe extension based on os.

String chromedriverPath="./src/main/resources/drivers/chromedriver";
if(System.getProperty("os.name").toLowerCase().contains("win"))
   chromedriverPath+=".exe";
System.setProperty("webdriver.chrome.driver",chromedriverPath);
WebDriver driver = new ChromeDriver();

or

You can simply handle driver download programmatically based on os version using Webdriver Manager

Add this jar dependency to your pom,

<dependency>
    <groupId>io.github.bonigarcia</groupId>
    <artifactId>webdrivermanager</artifactId>
    <version>3.0.0</version>
    <scope>test</scope>
</dependency>

Then add this one line before initiating the driver. This will download appropriate driver version automatically and set the path variable at run time.

WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();

Upvotes: 4

Related Questions