Filed under Native Device Testing.

Using the Overview, Home and Back native buttons is pretty straight forward using WebDriver and Appium. Below there are the code examples for all the 3 buttons:

 

How to click the Back Button on Android with Selenium WebDriver and Appium

    public void clickBackButton(){
		((AndroidDriver)driver).pressKeyCode(AndroidKeyCode.BACK);
	}

How to click the Overview Button on Android with Selenium WebDriver and Appium

	public void clickOverviewButton(){
		((AndroidDriver<WebElement>)driver).pressKeyCode(AndroidKeyCode.KEYCODE_APP_SWITCH);
	}

How to click the Home Button on Android with Selenium WebDriver and Appium

	
	public void clickHomeButton(){
		((AndroidDriver<WebElement>)driver).pressKeyCode(AndroidKeyCode.HOME);
	}

 

LoadFocus.com is a cloud testing platform:

Filed under Apache JMeter Tutorials.

In order to define your own variables and reuse them in your tests, it’s easier to use the User Defined Variables from JMeter.

Here is how to create a variable and use it in an HTTP Request from your JMeter Test Plan.

 

Steps

1. Open JMeter (here is a more detailed post on how to install JMeter) and Add a Thread Group to your Test Plan

2. Add a HTTP Request Sampler to your Thread Group

3. Right-Click the Thread Group and add User Defined Variables Config Element in your JMeter test
1-add-user-defined-varibles-jmeter

 

4. Create a new variable: var1 with value www.example.com
2-add-variable-in-user-defined-variables-jmeter

 

5. Go to the HTTP Request and add the variable name where you want to be replaced with its value, use ${var1}
3-user-variable-in-http-request-jmeter

 

6. Add a View Results Tree Listener in order to easily see the results of your request.
4-view-results-jmeter

 

7. Make the request and you can see that the ${var1} was replaced with www.example.com in the HTTP Request
5-inspect-results-jmeter

 

8. Add the ${var1} also in the name of the HTTP Request sampler and you can see the request has the value of the user defined variable var1
6-use-user-defined-variable-second-time-jmeter
7-inspect-http-requests-jmeter

 

Notes:

  • suggestion: for simplicity use User Defined Variables only at the beginning of a Thread Group
  • all User Defined Variables from a test plan are processed at the beginning no matter where they are added or placed in the JMeter Test Plan
  • JMeter User Defined Variables should not be used with functions that generate different results each time they are called
  • use User Parameters for defining variables during a test run instead of User Defined Variables
  • User Defined Variables are processed in the order they are added in the test plan, from TOP to BOTTOM
  • If, in your Test Plan,you have more than one Thread Group, use different names for different values, as UDVs are shared between Thread Groups.
  • You can reference variables defined in earlier UDVs or on the Test Plan.

 

Filed under Uncategorized.

If you want to scroll the page UP or DOWN with Selenium WebDriver in order to find web elements or make assertions, here are a few way you can do that.

How to scroll Page Down using Selenium WebDriver:

JavaScript scrollBy() method scrolls the document by the specified number of pixels. Update the second parameter of the scrollBy() method with the number of pixels in order scroll more or less.

The definition of the JavaScript method scrollBy() is:

window.scrollBy(xpixels,ypixels)

where:

  • xpixels is a Number and Required parameter:
    • How many pixels to scroll by, along the x-axis (horizontal).
    • Positive values will scroll to the left, while negative values will scroll to the right
  • ypixels is a Number and Required parameter:
    • How many pixels to scroll by, along the y-axis (vertical).
    • Positive values will scroll down, while negative values scroll up

 

In the examples we are using 250 pixels for vertical scrolling in order to show how scroll method works.


    @Test(groups = {"smoke"})
    public void test_Scroll_Page_Down() throws Exception {
        init();

        driver.navigate().to("http://www.alexa.com/topsites/countries;15/LU");

        JavascriptExecutor jse = (JavascriptExecutor) driver;
        jse.executeScript("window.scrollBy(0,250)", "");
    }

How to scroll Page UP using Selenium WebDriver:

Similar to scrolling Down the web page, but we set a negative number of pixels as the second parameter of the JS scrollBy() method to a negative number: -250

    @Test(groups = {"smoke"})
    public void test_Scroll_Page_UP() throws Exception {
        init();

        driver.navigate().to("http://www.alexa.com/topsites/countries;15/LU");

        JavascriptExecutor jse = (JavascriptExecutor) driver;
        jse.executeScript("window.scrollBy(0,-250)", "");
    }

How to scroll to the Bottom of the Web Page using Selenium WebDriver:

Basically, we get the height of the Body element from the DOM (Document Object Model) and we use the JavaScript method scrollTo() to scroll to the maximum height of the page:

    @Test(groups = {"smoke"})
    public void test_Scroll_Page_To_Bottom() throws Exception {
        init();

        driver.navigate().to("http://www.alexa.com/topsites/countries;15/LU");

        JavascriptExecutor jse = (JavascriptExecutor) driver;
        jse.executeScript("window.scrollTo(0, document.body.scrollHeight)");
    }

How to scroll Page to a Web Element Selenium WebDriver:

Just identify the Web Element fro the webpage and use the JS method scrollIntoView() to scroll the page to that web element until it becomes visible

    @Test(groups = {"smoke"})
    public void test_Scroll_Page_To_Element() throws Exception {
        init();

        driver.navigate().to("http://www.alexa.com/topsites/countries;15/LU");

        JavascriptExecutor jse = (JavascriptExecutor) driver;

        WebElement element = driver.findElement(By.linkText("Google.com.ph"));
        jse.executeScript("arguments[0].scrollIntoView();", element);
    }

How to scroll Page Down or UP using Keys Selenium WebDriver:

If you want to use the keys form the keyboard to scroll UP or DOWN you can use the WebDriver sendKeys() method and pass the PAGE_UP or PAGE_DOWN as an argument:

    @Test(groups = {"smoke"})
    public void test_Scroll_Page_Using_Keys() throws Exception {
        init();

        driver.navigate().to("http://www.alexa.com/topsites/countries;15/LU");

        Actions action = new Actions(driver);
        action.sendKeys(Keys.PAGE_DOWN);
        waitSeconds(2);
        action.click(driver.findElement(By.partialLinkText("Google.com.ph"))).perform();
    }

Hope you’ll find this useful. Check also how to find web elements using Selenium WebDriver.

LoadFocus is a cloud load testing platform. Try it for free.

Filed under Uncategorized.

Enroll Now for the new Online Course: How to Find XPath for Web Elements in Chrome and Firefox Browsers.

A lot of times while developing a test or when debugging a test you want to check to what element the XPath used in your test refers to. You will find this useful since you don’t need to install any other plugin or widget, you can use just built-in browser functionality.

For example, let’s imagine we have the following code in our UI Selenium test:

@FindBy(xpath = "//div[contains(@class,'item--lowest')]//a[contains(@class,'summary__date')]")
public WebElement summaryDateElement;

To identify to which element this refers to just do the following:

  1. Open the URL / web page in the Chrome browser
  2. Open the Web developer tools by pressing:
    •  Cmd + Alt + I (on Mac)
    • or by clicking View -> Developer -> Developer tools
    • or by Right-Click and Inspect Element
  3. Click on the Console tab in the Web developer tools
  4. Paste in the console the XPath from your test in the following format:
  5.  $x("//div[contains(@class,'item--lowest')]//a[contains(@class,'summary__date')]")
  6. Hovering with the mouse over the returned result will highlight the page element that the XPATH refers to.
  7. Details about locating elements with Selenium WebDriver can be found in our previous blog post.

That is it. You will see in the console what the XPpath returns.

Enjoy,
LoadFocus Team

P.S.: LoadFocus is a cloud load testing platform. Try it for free.

Filed under Uncategorized.

During UI test automation you will need to write tests that validate the options that are present in a select tag.
Getting all the options in the select tag is very simple using Selenium Webdriver and below is how it is done.

1. In the page object model of the page you create a WebElement that identifies the select tag; example class presented below:

import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

public class PageObject {
	
	public String[] occupantNoOptionExpectedArray = {"1", "2", "3", "4", "5+"};
	
	@FindBy(xpath = "//select[@id='numberofoccupants']")
	public WebElement noOfOccupantsDropdown;

}

2. In the test you will create a Select object (class org.openqa.selenium.support.ui.Select) like in the below example:

import java.util.List;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;
import org.testng.Assert;
import org.testng.annotations.Test;

public class OccupantPageTest {
	
	PageObject pageObject = new PageObject();
	
	@Test
    public void test_Occupants_Drop_Down_Values() throws Exception{
	     Select selectBedroom = new Select(pageObject.noOfOccupantsDropdown);
	     List<WebElement> optionSelect = selectBedroom.getOptions();
	                
	     for (int i = 0; i < optionSelect.size(); i++){
	      	Assert.assertEquals(optionSelect.get(i).getText(), pageObject.occupantNoOptionExpectedArray[i], "Values in the drop down for number of occupants are wrong"  );
	     }
    }
}

That’s it. Simple. Enjoy.

LoadFocus Team

Filed under Uncategorized.

In order to extract data from the response with JMeter you need to:

  • have the CSV file with the credentials in a format similar to the one below
  • have JMeter installed
  • JMeterPlugins-Extras installed – installing the plugins is detailed on the following post
  • JMeterPlugins-ExtrasLibs installed – installing the plugins is described on the following post

Four steps from the CSV file to being able to login and getting the token from the response

1. The CSV file with the credentials

The CSV file that we will use as the source(username and password) for the login process needs to be in a format similar to the following:

<strong>user1,password1</strong>

<strong>user2,password2</strong>

<strong>user3,password3</strong>

<strong>user4,password4</strong>

<strong>user5,password5</strong>

2. Create a new thread group and add the CSV Data Set Config as detailed in the images below

using_csv_for credentials1 using_csv_for credentials2

3. Add the JSON Path Extractor that we will use to extract the token from the response as in our case the response is in the JSON format like the following:

{

"data": {

"id": "a964a723ecfb61ba5e54d8cd22420f20",

},

"status": {

"httpStatus": 200,

"success": true

}

}
using_csv_for credentials3 using_csv_for credentials4

The expression that we use in the JSON Path Extractor is $.data.id as this refers to the id field in the data object. You need to adjust this to match your JSON format from the response.

4. Create the call that will access the area in the web application for which the user needs to be logged in and add the HTTP Header Manager that will use the token as a X-CSRF-Token like in the image below

using_csv_for credentials5 using_csv_for credentials6

You are all done, now you can access any part of your web application that needs the user to be logged in using the same process that we used at step 4.

Enjoy.

 

P.S.: LoadFocus is a cloud testing platform. Try it for free.

Filed under Uncategorized.

In order to make sure that your application will not break under a heavy load, a web application owner has to perform some basic performance tests.

Here are 4 simple steps how to achieve this with LoadFocus load testing tool:

  1. Log into your account on the LoadFocus platform
  2. Click on the “New Load Test” menu
  3. Enter the details of your web application (the endpoint/URL that you want to access, the number of clients per second and the number of seconds that the test will run) – check image attached for an example
  4. Before starting the test log into your server and use the script that we provide for monitoring the CPU and memory as below (the script is developed for Linux servers):
    1. copy the script cpu_mem_stats.sh to the server where your application is installed: click here to download script.
    2. run the following command:
      chmod 777 cpu_mem_stats.sh
    3. run the following command:
      ./cpu_mem_stats.sh -p node -t 200 -f /home/ubuntu/log.txt -c true
    4. the meaning of the parameters is (please modify according to your needs):
      • -p – The process name to look for
      • -t – The time to monitor specified in seconds
      • -f – path to the file to write the output (if not specified the output will be logged into the console)
      • -c – if specified as true will output in CSV format
  1. Start your load test from LoadFocus.com
  2. The result of the response times will give you a graph like this:
  3. After the test ends get the result from the stats test on your server (in our example log.txt)
  4. For creating the graphs from the result files we recommend the following online solution: https://plot.ly/ . From that you will get a graph similar to:
  5. Now by using the graphs from LoadFocus.com and the results from the cpu_mem_stats.sh you can correlate how you server is behaving under load, which will give you an idea if the machine needs to be upgraded or is enough to support the load your clients will generate.