WebDriver Browser Commands
Context:
The Browser commands are generally the ones which we intuitively feel that a browser should control. Examples are “type url and hit enter”, “getTitle”,”back”,”forward” and so on. Read on to find out what Selenium provides
Agenda:
- Print browser name
- Print browser ready state
- Perform back, forwards and refresh browser operations
1. GoTo (Go to a URL)
1 |
@browser.goto "http://www.seleniumframework.com" |
2. Browsername
1 |
@browser.name |
3. Browser ready state
1 |
@browser.ready_state |
4. Browser back
1 |
@browser.back |
5. Browser forward
1 |
@browser.forward |
6. Browser refresh
1 |
@browser.refresh |
7. Browser title
1 |
@browser.title |
8. Browser current url
1 |
@browser.url |
Steps
1. Open the template project in RubyMine
2. Right click on the features folder and create a feature file “browser_commands.feature”
3. Now copy the below gherkin code in “browser_commands.feature”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
Feature: Explaining browser commands Scenario: Print the browser name When I open seleniumframework website Then I print the browser name Scenario: Print the ready state When I open seleniumframework website Then I print the browser ready state Scenario: Browser back, forward and refresh When I open seleniumframework website And I click ABOUT link Then I click back on the browser And I click forward on the browser And I click refresh on the browser |
4. Now right-click on the step_definitions folder and create a new file “browser_commands.rb” [Watch the video at the end of this file to see how to do it step by step]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
Then(/^I print the browser name$/) do browserName = @browser.name puts "Browser name: #{browserName}" end Then(/^I print the browser ready state$/) do readyState = @browser.ready_state puts "Browser ready state: #{readyState}" end And(/^I click ABOUT link$/) do @browser.link(text: 'ABOUT').click end Then(/^I click back on the browser$/) do @browser.back puts "Browser title after back: #{@browser.title}" end And(/^I click forward on the browser$/) do @browser.forward puts "Browser title after forward: #{@browser.title}" end And(/^I click refresh on the browser$/) do @browser.refresh puts "Browser title after refresh: #{@browser.title}" end |
5. After adding both the browser_commands.feature and browser_commands.rb, your project should look like this
6. Now right click on each scenario (or if you wish to execute all scenarios) and run to see the output