Skip to main content

Selenium Browser Commands

To create a instance of a browser, 
WebDriver driver=new InternetExplorerDriver();
WebDriver driver=new FireFoxDriver();
WebDriver driver=new ChromeDriver();
You must have the driver file(which can be downloaded from the selenium site) in your local system for these commands to work. The files can be added in a place where your program can access them or you need to set their path in your program using commands like
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
System.setProperty("webdriver.ie.driver", "/path/to/iedriver");

Firefox driver comes with the selenium server jar so we don't have to download it separately.
                                                                         (or)
The following code can be used to execute scripts in the remote machine. The remote machine should have a selenium server instance running.

DesiredCapabilities capability=new DesiredCapabilities();
capability.setBrowserName("internet explorer");
//The name of the browser being used; should be one of {android|chrome|firefox|htmlunit|internet explorer|iPhone|iPad|opera|safari}WebDriver driver=new RemoteWebDriver(new URL("url of remote machine"),capability);

Get Command

Purpose: This command is use to open a new web page in the current browser.
Command: driver.get(URL);
Parameters: url – The URL to load. It is best to use a fully qualified URL

Get Title Command

Purpose: This command is use to get the title of the current page.
 Driver.getTitle();

Get Current URL Command

Purpose: This command is use to get the URL of the page currently loaded in the browser.
driver.getCurrentUrl() 

Get Page Source Command

Purpose: This command is use to get the source of the last loaded page.
driver.getPageSource()

Close Command

Purpose: This command is use to close the current window of the browser, if it’s the last window it will close the browser.
driver.close() 

Quit Command

Purpose: This command is use to quit the browser and all the opened windows in the browser.
driver.quit() 

Refresh Command

Purpose: This command is use to refresh the current browser.
driver.navigate().refresh();

Back Command

Purpose: This command is use to navigate back in the current browser.
driver.navigate().back ();

Forward Command

Purpose: This command is use to navigate forward in the current browser.
driver.navigate().forward ();

To Command

Purpose: This command is use to a page in the current browser.

driver.navigate().to (URL);

Aren't you wondering what's the difference between get(URL) and navigate().to(URL) commands?
The end result for both the commands is the same as in the page is loaded when the command is executed. The difference is the URL is stored in the navigation history when you use navigate().to() so you can use navigate().back() and navigate().forward() to go back and forth but when you use get() you don't have that option. 

Comments

Popular posts from this blog

How to Install Selenium and execute your first program

You can install and test Selenium using the following steps. All files can be downloaded from  http://www.seleniumhq.org/download/   Download Selenium Server jar file   Download client library java jar file   Download InternetExplorer Driver file   Install eclipse workspace. (download eclipse. Unzip folder. Find eclipse.exe application and open it) Create a new java project with 1,2 as references Copy 3 to workspace For IE, go to internet options->security and change the security level so that it’s the same for all zones. Write sample java program Below program opens google in IE and searches for selenium test and closes it. // All the imports happen automatically when you use eclipse ide and add the downloaded jars in the project references package test; import org.openqa.selenium.Alert; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.ie...

Transition from Test Automation Engineer to Test Engineer

What's the difference? Test Automation Engineer works on automating the Regression Test Suite. Test Engineer ensures the product can be released to the customer. With my industry experience, I have realized, that, as part of automation engineer your job duties don't stop with creating Automation tests, Frameworks and running them. With more tests being automated, your responsibilities not only include running the complete Regression suite but occasionally validation and verification of the product. There are different documents/sites defining different types of testing and where their usage, however,but finding bugs is a different skill. Though we are testers by role, our analytical minds work like developers as we develop scripts too. We could be biased. When something doesn't work, we look for a workaround and think it's no big deal. This attitude is a deal breaker!Instead use your skills to analyse and break the code. Here I am going to list some of my ideas ...

Form - User Input Automation

Text boxes WebElement t=driver.findElement(By.id(“username”)); t.sendkeys(“test name”); Buttons WebElement t=driver.findElement(By.id(“createbutton”)); t.click(); DropDown Boxes  WebElement select = driver.findElement(By.tagName("select")); List<WebElement> allOptions = select.findElements(By.tagName("option")); for (WebElement option : allOptions) { System.out.println(String.format("Value is: %s", option.getAttribute("value"))); option.click(); } This will find the first “SELECT” element on the page, and cycle through each of its OPTIONs in turn, printing out their values, and selecting each in turn. As you will notice, this isn’t the most efficient way of dealing with SELECT elements. WebDriver’s support classes include one called “Select”, which provides useful methods for interacting with these Select oSelection = new Select(driver.findElement(By.tagName("select"))); oSelection.deselectAll(); //All op...