Book Excerpt - Science of Selenium - Ch-7 Selenium Coding with JavaScript & Ruby

Book Excerpt - Science of Selenium - Ch-7 Selenium Coding with JavaScript & Ruby


Dear All

Using this forum to share some excerpts to raise the interest levels of the book I wrote and published through BPB Publications. I will share some excerpts every week to get your inputs and suggestions for future work.

This week, I am focusing on the Chapter - 7 Selenium Coding with JavaScript & Ruby.

Chapter 7 covers how to perform Selenium automation using programming languages such as Ruby and JavaScript.

Chapter 7
Selenium Coding with Other Languages (JavaScript, Ruby)

“The first rule of any technology used in a business is that automation applied to an efficient operation will magnify the efficiency. The second is that automation applied  to an inefficient operation will magnify the inefficiency.
– Bill Gates”


Introduction

Until now, we have looked at Selenium automation using the two most popular languages of choice, that is, Java and Python. In this chapter, we willlook at some of the concepts using other programming languages such as JavaScript and Ruby. We will also try to cover some programming examples in other languages (PHP, C#, etc.) that Selenium supports. Most of the concepts covered in this chapter will be a replication of the concepts covered earlier.

Structure

This chapter will cover programming examples in JavaScript, Ruby and other languages so that you can understand and explore the following  concepts:

●      Launch and closure of browsers

●      Browser level automation tasks, including –

  • URL/Web page navigation and URL information gathering
  • Page-level automation like page info gathering, element   identification and data capturing
  • CSS and XPathelement locators, selectors and locator ID customization
  • Management of Document Object Model  (DOM)
  • Simulation of screen sizes
  • UI element level automation, covering the following points  –
  • State inquiry, event handling, asynchronous interactions
  • UI  element  styling,  moving  between  frames  and   windows,
  • handling pop-ups and windows
  • History and locations, handling cookies, dragging and dropping Changing user agents
  • Fetching data from forms
  • Filling and submitting forms

....
....
....
INTENTIONALLY LEFT BLANK
....
....
....


How to Install and use Node.JS or JavaScript

TIP

You can download the Node.js installers for your development from the following sites:

●      https://nodejs.org/en/ – The site would give you the ideal version for your operating  system.

●      https://nodejs.org/en/download/ –The site would give you a choice to pick the installer or downloadable version suitable for your platform including add-ons, available in the downloader site.

You may download the latest version of your choice for installation. The installation process is very straightforward and self-explanatory. Since at the time of writing this book, the Selenium Alpha-5 version was the most apt choice, we have picked this up.

After the installation of Node.JS, you need to install Selenium- WebDriver to run your tests. This can be carried out by opening the Node.js command prompt and running the following  command:

npm install selenium-webdriver

As a next step, you need to ensure you have a good JavaScript- friendly integrated development environment. There are plenty of choices. Some of the top ones to consider are as  follows:

IntelliJ IDEA, MS Visual Studio Code, Komodo IDE, Koding, Eclipse with Node.JS Plug-in - nodeclipse, Atom and  Brackets

The programs used in this book were created using MS Visual Studio Code and Atom IDEs.

How to Install and use RUBY

TIPS

You can download the Ruby installers for your development from the following sites:

●      https://rubyinstaller.org/ – The sitewould give you a choice to pick the installer or downloadable version suitable for your platform including add-ons, available in the downloader  site.

You may download the latest version of your choice for installation. The installation process is very straightforward and self-explanatory. Since at the time of writing this book, the Selenium Alpha-5 version was the most apt choice, we have picked this up.

If you want to install any other gems (or libraries), you can use the

gem install command.

If you want to use the available developer kits or any other Ruby Gems (libraries for simple references), install them as  well.

ruby dk.rbinit and ruby dk.rb install

After the installation of Ruby, you need to install Selenium-WebDriver to run your tests. This could be carried out by opening the Ruby command prompt and running the following  command:

gem install selenium-webdriver

As a next step, you can choose to install any of the following libraries, if you want to test Selenium Grid, headless browser or perform Behaviour Driven Design (BDD) based testing using Cucumber  etc.

gem install rspec/gem install selenium-cucumber/gem install selenium-phantomjs/gem install selenium-grid

As a next step, you need to ensure you have a good Ruby-friendly

Integrated development environment. There are plenty of choices.

MS Visual Studio Code, Eclipse with Ruby-dltk, Atom, VIM, EMACS and JetBrains RubyMine are some

The programs used in this book were created using MS Visual Studio Code and RubyMine IDEs.
....
....
....
INTENTIONALLY LEFT BLANK
....
....
....


Selenium JavaScript Code Example & RUBYCode Example

Selenium JavaScript Code Example
 Selenium RUBYCode Example
....
....
....
INTENTIONALLY LEFT BLANK
....
....
....


Headless browser testing – Ruby

Similar to the JavaScript code in the preceding section, the code for Ruby is almost similar toa headless program. As a second method, we will use a class headless      by instantiating the object using the Headless.new and destroying it before the program is closed using the Headless.destroy functions. The rest of the code would exactly remain the same as the WebDriver program. In the program below, the use of the headless class commented using # tag, which covers both the type of headless program outputs.

require 'selenium-webdriver' 
require 'rspec/expectations'

def setup
   options = Selenium::WebDriver::Chrome::Options.new options.add_argument('--headless') options.add_argument('--disable-gpu')
   @driver = Selenium::WebDriver.for :chrome, options:options
end


def teardown
  @driver.quit
end

def run
 setup 
 yield 
 teardown
end

run do
 @driver.get "https://sites.google.com/view/crackingseleniuminterview/ home"
 puts(@driver.title)
 @driver.save_screenshot('Ch_7_Prog_7_Ruby_Headless.png') 
end


....
....
....
INTENTIONALLY LEFT BLANK
....
....
....


Conclusion

In this chapter, we have covered the key essential features of WebDriver automation using the JavaScript and Ruby programming languages. In the next chapter, we shall look at the conceptual approach, which is important for building a test automation framework using Selenium such as POM. In addition, we will also look at how we can bring the entire components of the test automation framework into onepicture. Having a strong grasp of the basic components of test automation, how to interweave them together to build a solid framework, having a strong approach for test automation and being flexible to accommodate the futuristic changes will  be a key to successful implementation. In the next chapter, we will discuss some of the advanced concepts such as how to establish a test automation framework and how to go about designing a solid approach for test  automation.

Questions

  1. How will you open a web browser using JavaScript and Selenium WebDriver?
  2. What is the use of WebElementPromise in JavaScript?
  3. What is the use of ThenableWebDriver Class?
  4. How will you use ActionChains in the Ruby programming  language?
  5. What is the difference between JavaScript and Ruby when it comes to the automation of Selenium WebDrivers?
  6. Where will you use Ruby and Python for web automation? What are the pros and cons of one over another?
  7. What are the key differences between Java and JavaScript when it comes to testing automation
  8. How will you automate headless browser testing using Ruby and  JavaScript in Selenium?

Conclusion

Hope you enjoyed the excerpts from Chapter-7. What are your comments?

Book Availability

The book is available at the following sites

Note:- Title Images are created using CANVA tools. Authors of the quote referred where known. Most of the information shared is generic and available in various forms in the Internet. Respective trademarks are owned by corresponding firms. Opinions about tools highlighted are from a personal experience standpoint and in no way reflect the views of my current or past employers or clients.

#WhatInspiresMe #Automation #Selenium #KRPoints #TestAutomation #DevOps #AITesting #NewAgeTesting #MobileTestAutomation

Rania Zervalaki Patrona

Empowering Brands to Stand Out, Scale Up, and Achieve Sustainable Growth.

5 年

Congratulations my dear friend Kalilur Rahman??

回复
Anne Thornley-Brown MBA

Team Building Expert | LinkedIn Top Voice | Forbes featured | I help executives manage change & foster innovation even in the midst of turbulence ???? ???? Actress ?? Writer ?? ???

5 年

That is why wee need to connect the dots and monitor what is happening inside and outside our business.

要查看或添加评论,请登录

Kalilur Rahman的更多文章

社区洞察

其他会员也浏览了