Executing Your Local Selenium Code

You are here:
< All Topics

Executing Local Selenium Code

Following is the sample Selenium code for reference:

package test;

import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.BrowserType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

public class WebDemo {
    static DesiredCapabilities cap;
    static URL url;
    static WebDriver driver;
    
    //Obtain Run URL from Link Provided for Organization
    static String TG_BROWSER_URL = "https://demo.testgrid.browsercloud/wd/hub";

    public static void main(String[] args) throws InterruptedException, MalformedURLException {
        cap = new DesiredCapabilities();
        //Set Browser Type as Needed
        cap.setBrowserName(BrowserType.CHROME);
        url = new URL(TG_BROWSER_URL);
        driver = new RemoteWebDriver(url, cap);
        driver.manage().window().maximize();
        //Write Selenium - Java Code for Automation
        driver.get("https://www.google.com/");
        driver.findElement(By.name("q")).sendKeys("Testgrid");
        driver.findElement(By.name("q")).sendKeys(Keys.ENTER);
        Thread.sleep(3000);
        driver.quit();

    }

}

 

Following variables needs to be changed as provided for organization & as per browser:

  • TG_BROWSER_URL
  • setBrowserName capability to one of the following:
    • BrowserType.CHROME
    • BrowserType.FIREFOX
    • BrowserType.OPERA_BLINK
    • BrowserType.EDGE
    • BrowserType.SAFARI

Additionally, Remote Execution of Code can also be viewed live on TestGrid Device Cloud.

Table of Contents