Planet Jaspersoft

Filter by tags:

May 19, 2020

Hi,

In this post, you will see the demonstration of "name" locator usage. 

"name" is one of the 8 locators supported by selenium, using it one can navigate to target page by performing click action(s).

For instance, Login to gmail  @ https://mail.google.com/
Let's see how to identify the name locators of login elements for gmail web application.

Email or Phone input text input HTML with "name" locator 
<input type="email" class="whsOnd zHQkBf" jsname="YPqjbf" 
       autocomplete="username" spellcheck="false" tabindex="0" 
       aria-label="Email or phone" name="identifier" value="" autocapitalize="none" 
       id="identifierId" dir="ltr" data-initial-dir="ltr" data-initial-value="">

Enter your password text input HTML with "name" locator
<input type="password" class="whsOnd zHQkBf" jsname="YPqjbf" 
       autocomplete="current-password" spellcheck="false" tabindex="0" 
       aria-label="Enter your password" name="password" autocapitalize="off" 
       dir="ltr" data-initial-dir="ltr" data-initial-value="">

Click on the image to get best view

selenium identifies the above input elements(username and password for gmail) using "name" locator with the following java statements. 
driver.findElement(By.name("identifier")).sendKeys("java.selenium2021@gmail.com");

WebElement elePassword=driver.findElement(By.name("password"));
elePassword
.sendKeys("JavaSelenium2021");
Take a look at the following example or watch this 1 min no voice video tutorial for end-to-end demo understanding and execution. 
nameLocatorDemo.java
//name locator demo
package selenium.locators.examples;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class nameLocatorDemo {

public static void main(String[] args) {

WebDriver driver
;

System
.setProperty("webdriver.chrome.driver", "D:\\006_trainings\\chromedriver.exe");
System
.setProperty("webdriver.chrome.silentOutput", "true");

driver
= new ChromeDriver();
driver
.navigate().to("https://mail.google.com/");

driver
.manage().window().maximize();

// locate "Email or Phone" text input with "name" locator and enter email say --> java.selenium2021@gmail.com

driver.findElement(By.name("identifier")).sendKeys("java.selenium2021@gmail.com");

// click on "Next" button - This is an xpath example that will be covered in later sessions
driver
.findElement(By.xpath("//span[@class='RveJvd snByac']")).click();

driver
.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

//locate "Enter your password" text input with "password" locator and enter password say --> JavaSelenium2021
WebElement elePassword=driver.findElement(By.name("password"));
elePassword.sendKeys("JavaSelenium2021");


elePassword
.sendKeys(Keys.TAB);
elePassword
.sendKeys(Keys.TAB);

// click on "Next" button - This is again an xpath example.
driver
.findElement(By.xpath("//span[contains(text(),'Next')]")).click();

//close the driver
//driver.close();
}
}


- Sadakar Pochampalli

May 19, 2020

Hi,

In this post, you will see the demonstration of "id" locator usage. 

"id" is one of the 8 locators supported by selenium, using it one can navigate to target page by performing click action.

For instance, Login to facebook site @ http://www.facebook.com
Let's see how to identify the ids of login elements for facebook web application.

"Email or Phone" text input HTML
<input type="email" class="inputtext login_form_input_box" name="email" id="email" data-testid="royal_email">

"Password" text input HTML
<input type="password" class="inputtext login_form_input_box" name="pass" id="pass" data-testid="royal_pass">

"Log In" button HTML
<input value="Log In" aria-label="Log In" data-testid="royal_login_button" type="submit" id="u_0_b">


The following statements identifies the above 3 elements using id attribute on the page.
 driver.findElement(By.id("email")).sendKeys("test123@gmail.com");
 driver.findElement(By.id("pass")).sendKeys("testPassword");
  driver.findElement(By.id("u_0_b")).click();.
Take a look at the following example or watch this ~2 min no voice video tutorial for end-to-end execution. 
idDemo.java
package selenium.locators.examples;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

//id demo
public class idDemo {

public static void main(String[] args) {

WebDriver driver
;

System
.setProperty("webdriver.chrome.driver", "D:\\006_trainings\\chromedriver.exe");
System
.setProperty("webdriver.chrome.silentOutput", "true");

//chrome driver object
driver
=new ChromeDriver();

driver
.get("https://www.facebook.com");

//maximizing the URL
driver
.manage().window().maximize();

//finding email element by "id" locator and entering email
driver
.findElement(By.id("email")).sendKeys("test123@gmail.com");

//finding pass element by "id" locator and entering password
driver
.findElement(By.id("pass")).sendKeys("testPassword");

//finding "Login" element by "id" locator and performing click action
driver
.findElement(By.id("u_0_b")).click();

//driver.manage().timeouts().implicitlyWait(20000, TimeUnit.SECONDS);

//driver.quit();

}
}

- Sadakar Pochampalli

May 19, 2020

May 18, 2020

Hi,

In this post, you will see the demonstration of "linkText" locator usage. For the purposes, I took my blog-site https://jasper-bi-suite.blogspot.com/.

linkText is one of the 8 locators supported by selenium, using it one can navigate to target page by performing click action.

For instance, navigate to "Services" page from the site.
Let's see how to perform/navigate to Services page using "Services" text from the following HTML script.
<a href="https://jasper-bi-suite.blogspot.com/p/contact-information.html">Services </a>

The following statement identifies "Services" link on the page and does the click action.
 driver.findElement(By.linkText("Services")).click();

Take a look at the complete java code at the bottom or watch below 1 min video tutorial for end-to-end execution.

Screenshot: 

Watch this 1 min video tutorial ( change quality to 720p)
linkTextDemo.java
package selenium.locators.examples;
//linkText example
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class linkTextDemo {

public static void main(String[] args) {

WebDriver driver
;

System
.setProperty("webdriver.chrome.driver", "D:\\006_trainings\\chromedriver.exe");
System
.setProperty("webdriver.chrome.silentOutput", "true");

driver
= new ChromeDriver();

driver
.navigate().to("https://jasper-bi-suite.blogspot.com/");
driver
.manage().window().maximize();

driver
.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

//linkText
driver.findElement(By.linkText("Services")).click();

JavascriptExecutor js
= (JavascriptExecutor) driver;
js
.executeScript("window.scrollBy(0,550)");

//driver.close();

}
}


- Sadakar Pochampalli 

May 18, 2020

Hi ,

This tutorial is intended to written for beginners and in this example, we would see how to set-up and launch a website in chrome browser (for example: https://jasper-bi-suite.blogspot.com/) using java selenium.

1. Download Eclipse zip from  https://www.eclipse.org/downloads/
2. Install java 1.8 or later and set path & class path in windows os.
3. Download suitable chromedriver.exe from https://chromedriver.chromium.org/downloads
      NOTE : This example is tested on : Version 81.0.4044.138 (Official Build) (64-bit)
4. Download selenium jar files and configure in Eclipse Build Path as (Referenced Libraries)
      https://www.selenium.dev/downloads/
5. Write FirstTestCase.java class with the code shown in below.
6. Run as "Java Applicaiton".

In the java class, ensure to have the following : 
1)  System set property  -  using this call the chromedriver.exe
2)  Declaring driver - driver declaration
3)  navigate().to - method to navigate to a given site.
4)     ChromeOptions options = new ChromeOptions();
         .......... Google Chrome Browser Options.............
        driver = new ChromeDriver(options);

Watch this ~15 sec video for execution of this case.



import java.util.concurrent.TimeUnit;
/**
* @author Sadakar Pochampalli
*/
import org.openqa.selenium.PageLoadStrategy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

public class FirstTestCase {

public static void main(String[] args) {

System.setProperty("webdriver.chrome.driver", "D:\\006_trainings\\chromedriver_win32\\chromedriver.exe");

WebDriver driver ;

ChromeOptions options = new ChromeOptions();

//options.addArguments("--headless");
options.addArguments("--disable-gpu");
options.addArguments("start-maximized");
options.setPageLoadStrategy(PageLoadStrategy.NONE);
options.addArguments("--allow-insecure-localhost");
options.addArguments("--allow-running-insecure-content");
options.addArguments("--ignore-certificate-errors");
options.addArguments("--no-sandbox");
options.addArguments("--window-size=1280,1000");
options.setCapability("acceptSslCerts", true);
options.setCapability("acceptInsecureCerts", true);
  driver = new ChromeDriver(options);

driver.navigate().to("https://jasper-bi-suite.blogspot.com/");
driver.manage().window().maximize();

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//driver.close();

}
}

May 18, 2020

May 11, 2020

In C#, if you try to print a non-static variable from static method, you will see the reference error message.


Severity Code Description Project File Line Suppression State
Error CS0120 An object reference is required for the non-static field, method, or property 'Car.color' ConsoleApp3 C:\Users\sadakar\source\repos\ConsoleApp3\ConsoleApp3\Oops\ClassesAnbObjects\Car.cs 15 N/A


I wanted to check this example both in C# and Core Java and so if you are wondering how the similar behavior appears in core  java , take a look at this example@


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp3.Oops.ClassesAnbObjects
{
class Car
{
string color = "red";
static void Main(string[] args)
{
Car myObj =
new Car();
Console.WriteLine(
color);

Console.ReadLine();
}
}
}

Solution:
1) Create an Object
2) Call/print the non static variable using object.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp3.Oops.ClassesAnbObjects
{
class Car
{
string color = "red";
static void Main(string[] args)
{
Car myObj = new Car();
Console.WriteLine(
myObj.color);

Console.ReadLine();
}
}
}




References: 
https://stackoverflow.com/questions/498400/cs0120-an-object-reference-is-required-for-the-nonstatic-field-method-or-prop

May 11, 2020

If you try to  access an instance variable or say non-static variable in a static method, you would end up with static reference error message.

For instance, in the following example, variable "a" is class variable/non-static instance variable and if accessed directly in the static method, say in this case it is main method, it would throw the reference exception.

NonStaticDataMemberInStaticMethod.java
package static1.method;

public class NonStaticDataMemberInStaticMethod {

int a=10 ;// non static variable
public static void main(String args[]) {
System
.out.println(a);
}

}


Error Message:
Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
Cannot make a static reference to the non-static field a

at static1.method.NonStaticDataMemberInStaticMethod.main(NonStaticDataMemberInStaticMethod.java:7)

Solution:
1) Create an Object
2) Call/print the non static variable using object.

package static1.Variable;

public class NonStaticDataMemberInStaticMethod {

int a =10;//non static variable
public static void main(String args[]) {

NonStaticDataMemberInStaticMethod myObj=new NonStaticDataMemberInStaticMethod();
System
.out.println(myObj.a);
}

}




- Sadakar Pochampalli 

May 11, 2020

April 30, 2020

Hi,

If we try to provide tags individually as below in main method for command line usage, we may end-up getting the error message saying .. None of the features at classpath matched the filters.

"-t","@scenario_AddMarketingAccount,
"-t","@scenario_AddMarketingUser",


None of the features at [classpath:features] matched the filters: [@scenario_AddMarketingAccount, @scenario_AddMarketingUser]

0 Scenarios

0 Steps
0m0.000s

Provide a single tag with comma separated literals/scenario tags. 
i.e., 
"-t","@scenario_AddMarketingAccount,@scenario_AddMarketingUser"

Below is how the tags should be written for CLI (command line interface)

package com.sadakar.selenium.common;
import org.openqa.selenium.WebDriver;

public class BasePage {

public static WebDriver driver;

public static void main(String args[]) throws Throwable{
try {
cucumber
.api.cli.Main.main(
new String[]{
"classpath:features",
"-g", "com.sadakar.cucumber.stepdefinitions/",
"-g","com.sadakar.cucumber.common",
"-t","@scenario_AddMarketingAccount,@scenario_AddMarketingUser",
"-p","pretty",
"-p","json:target/cucumber-reports/Cucumber.json",
"-p", "html:target/cucumber-reports",
"-m"
}
);
}
catch(Throwable e) {
e
.printStackTrace();
System
.out.println("Main method exception");
}
}
}


NOTE : 
When you run executable jar file, it executes ALL the scenarios given with "-t" tag.

1) Build the jar file 
clean compile assembly:single install

2) Run the jar file
java -DtestEnv=local -jar SADAKAR_POC-0.0.1-SNAPSHOT-jar-with-dependencies.jar

April 30, 2020

Hi,

Syntax: 
java -Dcucumber.options="-tags @<ScenarioName>" - jar <executable-jar-file>

java  -Dcucumber.options="--tags @scenario_AddUser" -jar SADAKAR_CUCUMBER_POC-0.0.1-SNAPSHOT-jar-with-dependencies.jar

Snapshot:




References: 
https://stackoverflow.com/questions/37559861/run-a-single-cucumber-scenario-from-an-executable-jar

April 30, 2020


Label content on the web:

xpath: 
//legend[contains(.,'Add Marketing User')]

HTML inspector :


April 30, 2020

April 22, 2020

Hi,

Use case : In a Store page form,
PASS the test : if the store belongs to an internal office
FAIL the test : if the store belongs to external office for sales

NOTE : While creating the Store in the form selected the checkbox so it is an internal store.

label and input type : Is Internal Store ( it is a check box )

@Then("^I verify all the Store details with the values given while creating a new Store$")
public void i_verify_all_the_Store_details_with_the_values_given_while_creating_a_new_Store() throws Throwable {
try {
Log.debug("Comparing Internal Store with the selection while a new Store is created");
Log.debug("Internal Store value while the Store is created is=true");
Log.debug("Get Value of internal Store from UI: "+driver.findElement(By.xpath("//*[@id='isInternalStore']")).getAttribute("value"));

if(
(driver.findElement(By.xpath("//*[@id='isInternalStore']")).isSelected()) &&
(driver.findElement(By.xpath("//*[@id='isInternalStore']")).getAttribute("value").toString().equals("true"))
) {
Log.debug("Store is equals with the selection while account is created");
}else
{
Assert.fail("else block.......... Store is NOT internal one");
System.out.println("Store is NOT internal one");
}
}catch(Exception e) {
Assert.fail("catch block.........Store is NOT internal one"+e);
System.out.println("Store is NOT internal one"+e);
}
}

- Sadakar Pochampalli 

April 22, 2020

Pages

Feedback
randomness