Thursday, 27 September 2018

Working on PushButtons and WebLinks - Examples to Practice

5. Working on Pushbuttons
---------------------------------
we can use click() command to click on pushbutton
we can use isEnabled() command to check object is enabled/disabled on a page

Exp:  Write script to click on "Sign up" button when that object is
enabled and also read name and visibletext of the object

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

//create reference object for "Sign up"
WebElement signupObj=driver.findElement(By.id("u_0_11"));

//to read visibletext of object
String btnText=signupObj.getText();
System.out.println("Object visibletext is: "+btnText);

//to read name attribute of object
String btnName=signupObj.getAttribute("name");
System.out.println("Object name is: "+btnName);

//to check object is enabled or not
if (signupObj.isEnabled()){
System.out.println("Object is enabled");
signupObj.click();
}
else{
System.out.println("Object is disabled");
}


6. Working with Links
----------------------------
In general links tagname will be “a” (anchor tag)
To address links we can use "linkText" or "partialLinkText"
-click(): command used to click on the links
-getText(): command used to read innertext property value of the link

Exp:  Write script to find number of links available on www.facebook.com home page and print those link names

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

List  links=driver.findElements(By.tagName("a"));
System.out.println("Number of links are: "+links.size());
//to print each link name
for(WebElement link: links){
System.out.println(link.getText());
}

Exp: Write script to find and click on "REGISTER" link from NewTours home page
String myLink="Register";
WebDriver driver= new FirefoxDriver();
driver.get("http://newtours.demoaut.com");
driver.manage().window().maximize();

List links=driver.findElements(By.tagName("a"));


//to read each link visibletext
for(WebElement link: links){
if(link.getText().equalsIgnoreCase(myLink)){
link.click();
System.out.println("specified link exist");
break;
}
}
Exp: Write script to find number of language links in www.google.co.in
WebDriver driver= new FirefoxDriver();
driver.get("http://google.co.in");
driver.manage().window().maximize();


//create reference object for parent object of language links
WebElement myLanguages=driver.findElement(By.id("SIvCob"));
//to find number of language links in a page
List links=myLanguages.findElements(By.tagName("a"));
System.out.println("Number of language links are: "+links.size());

//to read each link visibletext
for(WebElement link: links){
System.out.println(link.getText());
}

No comments:

Post a Comment