воскресенье, 17 июня 2018 г.

AQA: Java – Get Random Item/Element From a List

Last modified: May 5, 2018, by

1. Introduction

Picking a random List element is a very basic operation but not so obvious to implement. In this article, we’ll show the most efficient way of doing this in different contexts.

2. Picking a Random Item/Items

In order to get a random item from a List instance, you need to generate a random index number and then fetch an item by this generated index number using List.get() method.
The key point here is to remember that you mustn’t use an index that exceeds your List’s capacity.


2.1. Single Random Item

In order to select a random index, you can use Random.nextInt(int bound) method:

public void givenList_shouldReturnARandomElement() {
    List<Integer> givenList = Arrays.asList(1, 2, 3);
    Random rand = new Random();
    int randomElement = givenList.get(rand.nextInt(givenList.size()));
}
Instead of Random class, you can always use static method Math.random() and multiply it with list size (Math.random() generates Double random value between 0 (inclusive) and 1 (exclusive), so remember to cast it to int after multiplication).

вторник, 5 июня 2018 г.

AQA: Selenium - Check Visibility of Web Elements Using Various Types WebDriver Commands – Selenium Tutorial #14

How to check visibility of web elements using various types of looping and conditional commands in WebDriver:
Previously in the series, we discussed about WebDriver’s Select class which is primarily used to handle web elements like dropdowns and selecting various options under the dropdowns.
Moving ahead in the Selenium series, we would be discussing about the various types of looping and conditional commands in WebDriver like isSelected(), isEnabled() and isDispalyed(). These methods are used to determine the visibility scope for the web elements.
So let us start with a brief introduction – WebDriver has a W3C specification that details out the information about the different visibility preferences based out on the types of the web elements upon which the actions are to be performed.
WebDriver facilitates the user with the following methods to check the visibility of the web elements. These web elements can be buttons, drop boxes, checkboxes, radio buttons, labels etc.
  • isDisplayed()
  • isSelected()
  • isEnabled()
For an improved understanding, let us discuss the aforementioned methods with code examples.
As a specimen, we would be using the “google.com” as an application under test and the “Learning_Selenium” project created in the previous tutorials for script generation.
Scenario to be automated
  1. Launch the web browser and open the application under test – http://google.com
  2. Verify the web page title
  3. Verify if the “Google Search” button is displayed
  4. Enter the keyword in the “Google Search” text box by which we would want to make the request
  5. Verify that the “Search button” is displayed and enabled
  6. Based on visibility of the Search button, click on the search button
What You Will Learn: [show]

WebDriver Code

Step 1: Create a new java class named as “VisibilityConditions” under the “Learning_Selenium” project.
Step 2: Copy and paste the below code in the “VisibilityConditions.java” class.
Below is the test script that is equivalent to the above-mentioned scenario:
1import org.openqa.selenium.By;
2import org.openqa.selenium.WebDriver;
3import org.openqa.selenium.WebElement;
4import org.openqa.selenium.firefox.FirefoxDriver;
5 
6public class VisibilityConditions {
7 
8       /**
9        * @param args
10        */
11 
12       public static void main(String[] args) {
13 
14              // objects and variables instantiation
15              WebDriver driver = new FirefoxDriver();
16              String appUrl = "https://google.com";
17 
18              // launch the firefox browser and open the application url
19              driver.get(appUrl);
20 
21              // maximize the browser window
22              driver.manage().window().maximize();
23 
24              // declare and initialize the variable to store the expected title of the webpage.
25              String expectedTitle = "Google";
26 
27              // fetch the title of the web page and save it into a string variable
28              String actualTitle = driver.getTitle();
29 
30              // compare the expected title of the page with the actual title of the page and print the result
31              if (expectedTitle.equals(actualTitle))
32              {
33                     System.out.println("Verification Successful - The correct title is displayed on the web page.");
34              }
35              else
36              {
37                     System.out.println("Verification Failed - An incorrect title is displayed on the web page.");
38              }
39 
40              // verify if the “Google Search” button is displayed and print the result
41              boolean submitbuttonPresence=driver.findElement(By.id("gbqfba")).isDisplayed();
42              System.out.println(submitbuttonPresence);
43 
44              // enter the keyword in the “Google Search” text box by which we would want to make the request
45              WebElement searchTextBox = driver.findElement(By.id("gbqfq"));
46              searchTextBox.clear();
47              searchTextBox.sendKeys("Selenium");
48 
49              // verify that the “Search button” is displayed and enabled
50              boolean searchIconPresence = driver.findElement(By.id("gbqfb")).isDisplayed();
51              boolean searchIconEnabled = driver.findElement(By.id("gbqfb")).isEnabled();
52 
53              if (searchIconPresence==true && searchIconEnabled==true)
54              {
55                     // click on the search button
56                     WebElement searchIcon = driver.findElement(By.id("gbqfb"));
57                     searchIcon.click();
58              }
59 
60              // close the web browser
61              driver.close();
62              System.out.println("Test script executed successfully.");
63 
64              // terminate the program
65              System.exit(0);
66       }
67}

Code Walkthrough

Following are the ways in which we ascertain the presence of web elements on the web page.
boolean submitbuttonPresence=driver.findElement(By.id(“gbqfba”)).isDisplayed();

isDispalyed()

isDisplayed() is the method used to verify presence of a web element within the webpage. The method is designed to result from a Boolean value with each success and failure. The method returns a “true” value if the specified web element is present on the web page and a “false” value if the web element is not present on the web page.
Thus the above code snippet verifies for the presence of submit button on the google web page and returns a true value if the submit button is present and visible else returns a false value if the submit button is not present on the web page.
boolean searchIconEnabled = driver.findElement(By.id(“gbqfb”)).isEnabled();
The method deals with the visibility of all kinds of web elements not just limiting to any one type.

isEnabled()

isEnabled() is the method used to verify if the web element is enabled or disabled within the webpage. Like isDisplayed() method, it is designed to result in a Boolean value with each success and failure. The method returns a “true” value if the specified web element is enabled on the web page and a “false” value if the web element is not enabled (state of being disabled) on the web page.
Thus the above code snippet verifies if the submit button is enabled or not and returns a Boolean value depending on the result.
The isEnabled() method is significant in scenarios where we want to ascertain that only if “Condition A” is fulfilled, then the element(principally button) is enabled. Refer the following illustration for the same.
Webdriver commands 1
In the above figure, Register button is enabled only when the agreement checkbox is selected.
Akin to above methods, we have a method referenced as “isSelected()” which tests if the specified web element is selected or not.
boolean searchIconSelected = driver.findElement(By.id(“male”)).isSelected();

isSelected()

isSelected() is the method used to verify if the web element is selected or not. isSelected() method is pre-dominantly used with radio buttons, dropdowns and checkboxes. Analogous to above methods, it is designed to result a Boolean value with each success and failure.
Thus the above code snippet verifies if the male radio button is selected or not and returns a Boolean value depending on the result. Refer the following image for the same.

Conclusion

In this tutorial, we tried to make you acquainted with the WebDriver’s looping and conditional operations. These conditional methods often deal with almost all types of visibility options for web elements.
Article Summary:
  • WebDriver has a W3C specification that details out the information about the different visibility preferences based out on the types of the web elements.
  • isDisplayed() is the method used to verify a presence of a web element within the webpage. The method returns a “true” value if the specified web element is present on the web page and a “false” value if the web element is not present on the web page.
  • isDisplayed() is capable to check for the presence of all kinds of web elements available.
  • isEnabled() is the method used to verify if the web element is enabled or disabled within the webpage.
  • isEnabled() is primarily used with buttons.
  • isSelected() is the method used to verify if the web element is selected or not. isSelected() method is predominantly used with radio buttons, dropdowns and checkboxes.
Next Tutorial #15: While working on web applications, often we are re-directed to different web pages by refreshing the entire web page and re-loading the new web elements. At times there can be Ajax calls as well. Thus, a time lag can be seen while reloading the web pages and reflecting the web elements. Thus, our next tutorial in-line is all about dealing with such time lags by using implicit and explicit waits.
Note for the Readers: Till then, the reader can automate and test the visibility scope for the web elements using WebDriver’s methods.

SEO: Эксперимент: как Яндекс и Google учитывают ключевые слова в URL

Эксперимент: как Яндекс и Google учитывают ключевые слова в URL Эксперимент: как Яндекс и Google учитывают ключевые слова в URL Дата пу...