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

Selenium Grid - Distributed Systems

Selenium grid is powerful and can be easily used with just few lines of code. A Grid is a network of computers which can be heterogeneous and geographically dispersed. A Selenium Grid is how a grid of computers are connected using Selenium and configured to perform a task. A task typically here is running a suite of test cases with different browsers in different platforms across geographically dispersed systems. The test cases can be run in parallel using Selenium. So if we have a grid of three computers the same set of test cases can be run in IE, Firefox and Chrome. To establish this we have three important components. 1) Hub 2) Node 3) Test Script A Hub acts like the server to which requests are sent . The Hub sends to request to various registered nodes. The HUB can be instantiated using command line by using the selenium server standalone jar file. Open command prompt, navigate to the folder which has the selenium server file and use this command. java -jar selen...

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 ...

Switch between browsers\windows and pop ups

These commands can be used to switch between windows, iterate through different windows opened by the webdriver, handle pop ups and frames in windows.  GetWindowHandle Command Purpose : To get the   window handle   of the current window. driver.getWindowHandle(); Returns an alphanumeric string GetWindowHandles Command Purpose : To get the   window handle   of   all   the current windows. Set<String> handle=Driver.getWindowHandles(); Returns a set of window handles SwitchTo Window Command Purpose : WebDriver supports   moving   between named windows using the “switchTo” method. For(String handle:driver.getWindowHandles()) Driver.switchto().window(handle); Or Driver.switchto().window(windowname); <A href="newwindow.html" target=" windowname ">click here for a new window </A> SwitchTo Frame Command Purpose : WebDriver supports moving between frames using the “ switchTo ” method. Driver.s...