1

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 ?

Andrei Suvorkov
  • 5,559
  • 5
  • 22
  • 48
tupac shakur
  • 658
  • 1
  • 12
  • 29

1 Answers1

3

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();
Navarasu
  • 8,209
  • 2
  • 21
  • 32
  • I got it and if I want to run it from Linux in order to launch my program from Linux itself what modifications should I do? – tupac shakur Nov 06 '18 at 18:32
  • Download linux chrome driver version and put it under src/main/resources/drivers folder. Remove .exe extension in your setup code . like System.setProperty("webdriver.chrome.driver","./src/main/resources/drivers/chromedriver") – Navarasu Nov 07 '18 at 07:31
  • my final question is if I want to run this program from Linux server without UI is it possible? – tupac shakur Nov 07 '18 at 22:03
  • You can run chrome in headless mode. Refer this post. https://stackoverflow.com/questions/41836422/how-to-connect-to-chromium-headless-using-selenium. Also this blog http://total-qa.com/selenium-headless-chrome/. Just surf how to run chrome in headless to get help – Navarasu Nov 08 '18 at 10:55
  • I never tried it but I would click tick mark ... I used PhantomJSDriver in order to solve that problem which I did not need to use selenium server but only using this driver which provide me all the functionalities I needed. – tupac shakur Nov 09 '18 at 12:05
  • Yes thats also good option, if the application has less browser compatibility issue – Navarasu Nov 09 '18 at 12:23