Selenium Question 2



Q1. Print title of each site in web title?

System.out.println (driver1.gettitle())
Driver. close will close the current browsers.
Driver. quit  it will also do the same thing but it will close all the associated windows with it.
Driver.nevigate().to(“http:bbc.com”); this will also open the link, with navigate we can use key press event like back and forward buttons, but we cannot do with get.

Q2. what is a firefox profile ?

We want our own instance of firefox not the firefox used by every one though we have common browser.
Close Fire fox with exit in file menu.
Go to run and give command firefox.exe –profilemanager
A window will come  Click on create profile and chose the folder whe you want to save the profile.
Now start firefox, it will be brand new.
To open the same profile with selenium through eclipses.
ProfilesIni allprofiles = new ProfilesIni()
FireFoxProfile profiles = allprofiles.getprofile(“teas”)
ProfilesIni its an inbuld class that manages the profile, and it loads all the profile.
FireFoxProfile profiles it will get the profile created with name Abhinav.

Q3. HTML unit Driver.

It never opens the browser and test.
WebDriver driver  = new HtmlUnitDriver();

Q4. FireBug?

Its an addon to firefox, its like object spy in QTP. After installing firebug you can install firepath it’s a add-on over firefox, it show the Xpath of that button or any field
What ever thing start with A it’s a link in html.

Q5. To print all the values present in drop down?

Every thing comes under option box under a option tag of fire bug.
Webelement droplist = driver.findelement(by.xpath(“Give Xpath”))
Droplist.findelements(by.tagname(“option”));
It is returning a list so we have to write
List<WebElement> options = droplist.findelements(By.tagName(“option”));
System.out.println(option.size) // it will print the number of element
To print the element write a for loop and under it give
Webelemnt o = options.get(i);
System.out.println(o.gettext());
Or we can write
System.out.println(options.get(i).gettext())
We have value associated with each option
System.out.println(options.get(i).gettext() + “…..”+options.get(i).getattribute(“Value”);
Any thing on firebug with in tag <> is attribute.

Q6. How to handle radio buttons?

All radio buttons within a group will have same name but different value.
List<webelement> radio = driver.findelements(by.name(“group1”)
List will have all the radio buttons.
System.out.println(radio.get(0).getattribute(“Value”)+”….”+radio.get(0).getattribute(“checked”));
It will see is radio button checked or not.
Radio.get(0).click
It will click on first radio button.

Q7. Can we make our own Xpath?

Yes Ex:-
//*[@id=’Email’]
//input[@name = ‘Email’]
//input[@id=’Email’ and @name = ‘Email’]
Ctrl+shift+o :- all the imports come up

Q8. How to extract all the links from a web page?

List <webelement> alllinks = Driver.findelements(by.tagname(“a”))
‘tagname(“a”))
 As on firebug you can see all link tag name start with a.
System.out.println(alllinks.size()); // will print total number of link
For writing all the links use for loop
For (int i=0 ; i<alllinks.size();i++)
 System.out.println(alllinks.get(i).gettext);

Q9. How to extract limited links not all the links.

So related links lie under a common Xpath ex:-
Webelement footer = driver.findelement(By.xpath(“//*[@id=’footer’]”))
List <webelemnts> footerlink = Footer.findelements(by.tagname(“a”);
For (int i=0 ; i<fotterlink.size();i++)
 System.out.println(alllinks.get(i).gettext);

Q10. how to capture screenshorts with webdriver ?

File scrfile = ((takescreensort)driver).getscreensoretAs(outputtype.file))
Fileutil.copyfile(ScrFile, new file (“C:\\tmp\\screensort.jpg”));
Takescreensort is a class
Second command location is important.

No comments:

Post a Comment