Saturday, 22 September 2018

Working on EditBox and Dropdowns - Examples to Practice


Examples to Practice
--------------------------

1. Edit box/Text box:
In general, we use id or name as Locator to address Edit box/Text box
To work on Edit box we can use following commands
i. click(): to focus on specified edit box
ii.sendKeys(): to enter data into the edit box
iii. getAttribute("value"): to read data from the Edit box
iv. clear(): to clear the data from the edit box

Exp: Create script to automate following scenario:
open www.facebook.com in FF browser
focus on "Email" edit box
enter value "InsightQ" in "Email" edit box
read data from "Email" edit box
verify entered value matching with existing value in edit box
clear data in edit box

Script:
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://facebook.com");

String uid="InsightQ";

//creating reference object for webelement
WebElement we= driver.findElement(By.id("email"));
//to focus on webelement
we.click();
//to enter data
we.sendKeys(uid);
//to read data from edit box
String userid=we.getAttribute("value");
//to compare the values
if (userid.equals(uid)){
System.out.println("entered value existing");
}else{
System.out.println("values are mistaching");
}
//to clear the data
we.clear();

NOTES:  Reference/Instance object is used for a webelement in the script
for reusability of the webelement

Syntax:
WebElement obj= driver.findElement(By.Locator("locator value"));

Exp:
WebElement myObj= driver.findElement(By.id("Passwd"));
---------------------------------------------------------------------------------------------

2. Dropdowns:
Also called a list box/combobox
there are 2 types of dropdowns
i. Auto-suggested Dropdowns:
ii. Static Dropdowns:

i. Auto-suggested Dropdowns:
User can enter data as well as select value from search result
we can use click() method to operate this dropdown
sendKeys() method to enter data

Exp: Create script to automate following scenario:
open http://spicejet.com in FF browser
click on "From" dropdown box
select "GOA"

Script:
WebDriver driver= new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://spicejet.com");

driver.findElement(By.id("ctl00_mainContent_ddl_originStation1_CTXT")).click();
driver.findElement(By.linkText("Goa (GOI)")).click();
---------------------------------------------------------------------------------------------

Exp: Create script to automate following scenario:
open http://www.apsrtconline.in/oprs-web/ in FF browser
enter value "Hyd" in "From" dropdown box
click on "HYDERABAD MGBS"

Script:
WebDriver driver= new FirefoxDriver();
driver.get("http://www.apsrtconline.in/oprs-web/");
driver.manage().window().maximize();

//to enter value in "From" object
driver.findElement(By.name("fromPlaceName")).sendKeys("Hyd");

//to pause execution fixed time
Thread.sleep(3000);

//to select "HYDERABAD MGBS"
driver.findElement(By.linkText("HYDERABAD MGBS")).click();

---------------------------------------------------------------------------------------------

ii. **Static Dropdowns:
Under this, the values are fixed,
Static dropdown tagname will be "select"
To handle this type of webelements, WD providing "Select" class

Syntax:{for type-casting to the static dropdown}

Select obj= new Select(driver.findElement(By.locator("locator value"));

using following commands we can select value in Static dropdowns
i. obj.selectByVisibleText();
using this option we can select option in dropdown
using visibletext/value which is displayed in dropdown

ii. obj.selectByValue
using this option we can select value in dropdown
based on "value" attribute of option

iii. obj.selectByIndex();
sometimes we can also use index to select option
in dropdown (NOTE:  index starts with 0,1, 2,....)

iv. getOptions():
using this method we can get all the options information
from static dropdown

v. getText():
using this method we can read visibletext of options
from static dropdown

vi. **getFirstSelectedOption():
using this command we can read selected value from dropdown
Syntax:
obj.getFirstSelectedOption();


Exp:  Create script to automate following scenario:
open "http://facebook.com" in FF browser
select "Sept" using visible text
select "Apr" using value
select "Dec" using index

Script:
WebDriver driver= new FirefoxDriver();
driver.get("http://facebook.com");
driver.manage().window().maximize();

//type casting Select class
Select myList= new Select(driver.findElement(By.id("month")));

//to select "Sept" using visible text
myList.selectByVisibleText("Sept");

Thread.sleep(3000);

//to select "Apr" using value
myList.selectByValue("4");

Thread.sleep(3000);
//to select "Dec" using index
myList.selectByIndex(12);

---------------------------------------------------------------------------------------------

Exp:  Write script to select "Jan" in "Month" dropdown if it is not selected in that dropdown
Exp:  Create script to automate following scenario:
open "http://facebook.com" in FF browser
read value from the dropdown
select "Jan" if its not selected

Script:
WebDriver driver= new FirefoxDriver();
driver.get("http://facebook.com");
driver.manage().window().maximize();

//type caste dropdown with select class
Select myList= new Select(driver.findElement(By.id("month")));
//read selected value from dropdown
WebElement option= myList.getFirstSelectedOption();
String month=option.getText();
System.out.println(month);
//if it is not selected
if (month.equals("Jan")){
System.out.println("Jan-month already selected");
}
else{
//to select "Jan"
myList.selectByVisibleText("Jan");
System.out.println("Month slected by WD");
}


---------------------------------------------------------------------------------------------

NOTE: To find number of values in dropdown we can use getOptions() command
NOTE: To read values from dropdown we use getText()

Exp:  Create script to automate following scenario:
open "http://spicejet.com" in FF browser
select first item from the currency dropdown
select "INR" using value
select "AED" using visibletext
find number values under currency dropdown
print all currency values one by one


Script:
WebDriver driver= new FirefoxDriver();
driver.get("http://spicejet.com");
driver.manage().window().maximize();

//type-casting to "Currency" dropdown
Select myDropDown=new Select(driver.findElement(By.id("ctl00_mainContent_DropDownListCurrency")));


//to select 1st item
myDropDown.selectByIndex(0);

//to pause the execution
Thread.sleep(2000);

//to select item which have the value as "INR"
myDropDown.selectByValue("INR");

Thread.sleep(2000);

//to select item by visibletext "AED"
myDropDown.selectByVisibleText("AED");

//to find number of values
List items= myDropDown.getOptions();

System.out.println("Number of values are: "+items.size());

//to print those values
for(WebElement item: items){
System.out.println(item.getText());
}
---------------------------------------------------------------------------------------------

Exp:
Create script to automate following scenario:
open "www.newtours.demoaut.com"
click on "Register" link
select "INDIA" using visibleText in "Country" dropdown
select "ALBANIA" using index
select "INDIA" using value
find number of values in that dropdown
print all the values


Script:
WebDriver driver= new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://newtours.demoaut.com");

//to clickon Register link
driver.findElement(By.linkText("REGISTER")).click();

//create instance object for Country weblist (i.e. casting)
Select we= new Select(driver.findElement(By.name("country")));
//select "INDIA" using visibleText
we.selectByVisibleText("INDIA");
//to pause execution
Thread.sleep(2000);
//select "ALBANIA" using index
we.selectByIndex(0);
Thread.sleep(2000);
//select "INDIA" using value
we.selectByValue("92");
//find number of values in that dropdown
List options=we.getOptions();
System.out.println("number of values are: "+options.size());
//print all the values
for(WebElement option: options){
System.out.println(option.getText());
}

---------------------------------------------------------------------------------------------

Exp:
Create script to automate following scenario:
open "http://spicejet.com"
select first item from currency dropdown using index
select second item using visibleText
select last item using value
find number of values in that dropdown
print all the values

Script:
WebDriver driver= new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://spicejet.com");

Select myList= new Select(driver.findElement(By.id("ctl00_mainContent_DropDownListCurrency")));

//to select 1st option

myList.selectByIndex(0);

Thread.sleep(2000);
//to select 2nd option using visibletext

myList.selectByVisibleText("AED");
Thread.sleep(2000);
//to select last option using value
myList.selectByValue("USD");

//to find number of values in dropdown
List options= myList.getOptions();

System.out.println("no of options are: "+options.size());
for(WebElement x: options){
System.out.println(x.getText());
}

---------------------------------------------------------------------------------------------

Exp:
Create script to automate following scenario:
open "http://newtours.demoaut.com"
click on Register link
find number values under the country dropdown
check if "INDIA123" exist in country dropdown

Script:
WebDriver driver= new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://newtours.demoaut.com");

driver.findElement(By.linkText("REGISTER")).click();

Select myList= new Select(driver.findElement(By.name("country")));
String myCountry= "INDIA123";
//to find number of values in list box
List options=myList.getOptions();

boolean myStatus= false;
for(WebElement option:options ){
String myVal=option.getText();
if(myVal.equals(myCountry)){
myList.selectByVisibleText(myCountry);
myStatus=true;
break;
}
}

if (myStatus== false){
System.out.println(myCountry+" value not exist");

}

---------------------------------------------------------------------------------------------
Exp:
Create script to automate following scenario:
open "http://facebook.com"
read values from month dropdown
check if "Dec" is selected if not select "Dec"

Script:
WebDriver driver= new FirefoxDriver();
driver.get("http://facebook.com");
driver.manage().window().maximize();

Select birMonth=new Select(driver.findElement(By.name("birthday_month")));

//to read selected option from dropdown
WebElement option=birMonth.getFirstSelectedOption();
String x=option.getText();
System.out.println(x);
if(x.equals("Dec")){
System.out.println("Dec- already selected");
}
else{
//to select "Dec" using visibletext
birMonth.selectByVisibleText("Dec");
}

No comments:

Post a Comment