SeleniumFAQ With Ans
SeleniumFAQ With Ans
SeleniumFAQ With Ans
clicking button, going into pages, getting data from the browser itself, the sam
e thing for Firefox, IE, and so on.
9.
What is Selenium IDE
Ans- Selenium IDE is a complete integrated development environment (IDE) for Sel
enium tests.
It is implemented as a Firefox Add-On and allows recording, editing, and debuggi
ng tests.
It was previously known as Selenium Recorder.
Scripts may be automatically recorded and edited manually providing auto-complet
ion support and the ability to move commands around quickly.
Scripts are recorded in Selenese, a special test scripting language for Selenium
.
Selenese provides commands for performing actions in a browser (click a link, se
lect an option), and for retrieving data from the resulting pages.
Limitations of Selenium IDE:
a)It does not supports looping or conditional statements. Tester has to use nati
ve languages to write logic in the test case.
b)It does not supports test reporting, you have to use selenium RC with some ext
ernal reporting plugin like TestNG or JUint to get test execution report.
c)Error handling is also not supported depending on the native language for this
.
10.
What is Selenium Webdriver
Ans- WebDriver is the name of the key interface against which tests should be wr
itten in Java, the implementing classes one should use are listed as below:
AndroidDriver, ChromeDriver, EventFiringWebDriver, FirefoxDriver, HtmlUnitDriver
, InternetExplorerDriver, IPhoneDriver, PhantomJSDriver, RemoteWebDriver, Safari
Driver
11.
What is annotations
Ans- @Test, @BeforeTest, @AfterTest, @BeforeClass, @AfterClass, @BeforeMethod,
@AfterMethod, @BeforeSuite, @AfterSuite.
12.
What is TestNg?
Ans- TestNG is a unit testing framework for the Java programming language inspir
ed from JUnit and NUnit.
The design goal of TestNG is to cover all categories of tests: unit, functional,
end-to-end, integration, etc.,
with more powerful and easy-to-use functionalities.
13.
What is the benefit of using TestNG?
Ans1- TestNG allows us to execute of test cases based on group.
2- In TestNG Annotations are easy to understand
3- Parallel execution of Selenium test cases is possible in TestNG.
4- Three kinds of report generated
5- order of execution can be changed
6- failed test cases can be executed
7- without having main function we can execute the test method.
8- An xml file can be generated to execute the entire test suite.
In that xml file we can rearrange our execution order and we can also skip the e
xecution of particular test case.
14.
Write the code for Reading and Writing to Excel through Selenium
Ans-FileInputStream fis = new FileInputStream( path of excel file );
Workbook wb = WorkbookFactory.create(fis);
Sheet s = wb.getSheet(wb);
String value= s.getRow(rowNum).getCell(cellNum).getStringCellValue(); /
/ read
Reporter.log(value, true);
s.getRow(rowNum).getCell(cellNum).setCellValue();
//wri
wb.write(fos);
/write
te
/write
15.
Selenium function used for retrieving the attribute or value?
Ans- getAttribute(), getText().
16.
How do get typed text from a textbox?
Ans- driver.findElement(By.xpath("//input[@id='username']")).getAttribute("value
"));
17.
How do you differentiate check box if more than one check box is existed
in your application?
Ans- by writing the xpath in dynamic way.
18.
Difference between Assert and Verify?
AnsAssert- it is used to verify the result. If the test case fail then it will stop
the execution of the test case there itself and move the execution to other tes
t case.
Verify- it is also used to verify the result. If the test case fail then it will
not stop the execution of that test case.
19.
What is the alternate way to click on login button?
Ans- use submit() method but it can be used only when attribute type=submit.
20.
How to get the href of a link / get the source of image
Ans- use getAttribute() method.
21.
Count the number of links in a page.
Ans- use the locator By.tagName and find the elements for the tag //a then use l
oop to count the number of elements found.
Syntax- int count = 0;
List<webElement> link = driver.findElements(By.tagName( a ));
for(int i=0; i<=link.size(); i++)
{
count = count+1;
}
System.out.println(count); // this will print the number of links in a page.
22.
How do you verify if the checkbox/radio button is checked or not?
Ans- isSelected();
ex- driver.findElement(By.id("Rr")).isSelected();// return type is boolean
23.
How to check all checkboxes in a page
Ans- List<webElement> chkBox = driver.findElements(By.xpath( //htmltag[@attribute
='checkbox'] )); //input[@type='checkbox']
for(int i=0; i<=chkBox.size(); i++)
{
chkBox.get(i).click();
}
24.
How do you handle browser popup?
Ans- to handle browser popups , we need to move the control from one browser pop
up to other.
Syntax- Iterator<String> it = driver.getWindowHandles().iterator();
String parent = it.next();
String child = it.next();
driver.switchTo().window(child); //to move control to child browser
driver.switchTo().window(parent); //to move control back to parent bro
wser
There are 6 Diff types of PopupsAlert;
Hidden Div;
Page on Load;
File Upload;
File Download;
Window popup
25.
How do you handle Javascript alert/confirmation popup?
Ans- To handle popups, we need to 1st switch control to alert popups then click
on ok or cancel then move control back to main page.
Syntax- String mainPage = driver.getWindowHandle();
Alert alt = driver.switchTo().alert(); // to move control to alert popup
alt.accept(); ---> to click on ok.
alt.dismiss(); ---> to click on cancel.
Then move the control back to main web pagedriver.switchTo().window(mainPage); // to switch back to main page.
26.
How do you handle elements present inside frame?
Ans- to handle elements present in frame, 1st we need to move the control to fra
me then we can handle elements of frame.
Syntax to move control to frame- driver.switchTo().frame(webElement);
and to move the control back to main page driver.switchTo().defaultContent();
27.
How to get the number of frames on a page?
Ans- List <WebElement> framesList = driver.findElements(By.xpath("//iframe"));
int numOfFrames = frameList.size();
28.
How to verify that an element is not present on a page?
Ans- try
{
driver.findElement(locatorKey);
return true;
}
catch (org.openqa.selenium.NoSuchElementException e)
{
return false;
}
29.
How do you launch IE/chrome browser?
Ans- before launching IE or Chrome browser we need to set the System property.
To open IE browser // System.setProperty( ie.webdriver.driver , path of the iedriver.e
xe file );
WebDriver driver = new InternetExplorerDriver();
To open Chrome browser // System.setProperty( chrome.webdriver.driver , path of the ch
romeDriver.exe file );
WebDriver driver = new ChromeDriver();
30.
How to perform right click using WebDriver?
Ans- use Actions class.
Actions act = new Actions(driver);
act.moveToElement(webElement).perform();
act.contextClick().perform();
31.
How do perform drag and drop using WebDriver?
Ans- use Action class.
Actions act = new Actions(driver);
WebElement source = driver.findElement(By.xpath(
WebElement target = driver.findElement(By.xpath(
act.dragAndDrop(source,target).perform();
----- ));
----- ));
32.
How to send ENTER/TAB keys in WebDriver?
And- use click() or submit() [submit() can be used only when type='submit'])meth
od for ENTER.
Or act.sendKeys(Keys.ENTER);
For Tabact.sendKeys(Keys.TAB);
33.
Example for method overload in WebDriver
Ans- frame(string), frame(int), frame(WebElement)
34.
How do you upload a file?
using .sendKeys()
Ans- driver.findElement(By.xpath( input field )).sendKeys( path of the file which u wa
nt to upload );
35.
How do you click on a menu item in a drop down menu?
Ans- if that menu has been created by using select tag then we can use the metho
ds selectByValue() or selectByIndex() or selectByVisibleText().
These are the methods of the Select class.
If the menu has not been created by using the select class then we can simply fi
nd the path of that element and click on that to select.
36.
How do you simulate browser back and forward?
Ans- driver.navigate().back();
driver.navigate().forward();
37.
How do you get the current page URL?
Ans- driver.getCurrentUrl();
38.
What is the difference between findElement and findElements?
Ans- Both methods used to find the webElement in a web page.
findElement() - it used to find the one web element. It return only one WebElem
ent type.
findElements()- it used to find more than one web element. It return List of We
bElements.
39.
How do you achieve synchronization in WebDriver?
Ans- driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
40.
Explain implicit and explicit wait
Ansimplicit wait- is like to wait for specific time only when element is not found.
If element found then wait time is 0.
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS); here max
wait time is 10 sec.
explicit wait- is like to wait for specific time either element is found or not.
If element found then also wait for specified time.
Thread.sleep(time);
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(
By.id("someid")));
Difference between Implicit wait and Explicit wait?
Implicit Wait: During Implicit wait if the Web Driver cannot find it immediately
because of its availability,
the WebDriver will wait for mentioned time and it will not try to find the eleme
nt again during the specified time period.
Once the specified time is over, it will try to search the element once again th
e last time before throwing exception.
The default setting is zero. Once we set a time, the Web Driver waits for the pe
riod of the WebDriver object instance.
Explicit Wait: There can be instance when a particular element takes more than a
minute to load.
In that case you definitely not like to set a huge time to Implicit wait, as if
you do this your browser will going to wait
for the same time for every element.
To avoid that situation you can simply put a separate time on the required eleme
nt only.
By following this your browser implicit wait time would be short for every eleme
nt and it would be large for specific element
41.
What are the languages supported by WebDriver?
Ans- Python, Ruby, C# and Java are all supported directly by the development tea
m. There are also webdriver implementations for PHP and Perl.
42.
How do you clear the contents of a textbox in selenium?
Ans- use clear() method.
43.
What is a Framework?
Ans- A framework is set of automation guidelines which help in Maintaining consi
stency of Testing,
Improves test structuring, Minimum usage of code, Less Maintenance of code, Impr
ove re-usability,
Non Technical testers can be involved in code, Training period of using the tool
can be reduced, Involves Data wherever appropriate.
There are five types of framework used in software automation testing:
1-Data Driven Automation Framework
2-Method Driven Automation Framework
3-Modular Automation Framework
4-Keyword Driven Automation Framework
5-Hybrid Automation Framework = 1+2+3;
44.
Different components of your framework?
Ans- Library- Assertion, ConfigLibrary, GenericLibrary, ProjectSpecificLibrary,
Modules.
Drivers folder, Jars folder, excel file.
45.
How do you accommodate project specific methods in your framework?
Ans- 1st go through all the manual test cases and identify the steps which are r
epeating.
Note down such steps and make them as methods and write into ProjectSpecificLibr
ary.
46.
How is the failure handled in your framework?
Ans- We can execute the failure test case and identify where is the exact issue.
If this is a code issue then we raise the bug/defect for the developer and if it
s an script issue then we check the script and correct it.
47.
What kind of reports are generated by your framework?
Ans- Three kinds of report- TestNG report, Eclipse Console report and HTML repor
t.
48.
What are the prerequisites to run selenium webdriver?
Ans- JDK, eclipse, WebDriver(selenium standalone jar file), browser, application
to be tested.
49.
What are the advantages of selenium webdriver?
Ans- It supports with all browsers like Firefox, IE, Chrome, Safari, Opera etc.
Doesn t required to start server before executing the test script.
It actual core API which has binding in a range of languages.
It supports of moving mouse cursors.
It support to test iPhone/Android applications.
50.
What is WebDriverBackedSelenium?
Ans- WebDriverBackedSelenium is a kind of class name where we can create an obje
ct for it as below:
Selenium webdriver= new WebDriverBackedSelenium(WebDriver object name, "URl path
of website").
The main use of this is when we want to write code using both WebDriver and sele
nium RC, we must use above created object to use selenium commands.
51.
How to invoke an application in webdriver?
Ans- driver.get( url );
52.
What is Selenium Grid?
Ans- Selenium-Grid allows you to run your tests on different machines against di
fferent browsers in parallel.
That is, running multiple tests at the same time against different machines runn
ing different browsers and operating systems.
Essentially, Selenium-Grid support distributed test execution. It allows for run
ning your tests in a distributed test execution environment.
53.
How do you simulate scroll down action?
Ans- actions.sendKeys(Keys.HOME).perform();
actions.sendKeys(Keys.END).perform();
actions.sendKeys(Keys.UP).perform();
actions.sendKeys(Keys.DOWN).perform();
54.
what is the command line we have to write inside a .bat file to execute
a selenium project when we are using testng.
Ansjava -cp bin;jars/* org.testng.TestNG testng.xml
55.
How to verify the presence of the success message on a page?
Ans- getText();
56.
Which is the package which is to be imported while working with webdrive
r?
Ans- org.openqa.selenium
57.
How to check if an element is visible on the web page?
Ans- driver.findElement(By.xpath("//input[@id='username']")).isDisplayed();
58.
How to check if a button is enabled on the page?
Ans- use getAttribute() method to get the value of attribute which developer int
roduced in the web page to identify whether button is enabled or disabled.
exif(driver.findElement(By.xpath("//button[text()='OK']")).getAttribute("aria-disa
bled").equals("false"))
{
Globals.driver.findElement(By.className("x-panel-btns")).findElement(By.
xpath("//button[text()='OK']")).click();
}
else
{
Globals.driver.findElement(By.className("x-panel-btns")).findElement(By.
xpath("//button[text()='Cancel']")).click();
}
59.
How to check if a text is highlighted on the page?
Ans- To identify weather color for a field is different or notString color = driver.findElement(By.xpath("//a[text()='Shop']")).getCssValue("c
olor");
String backcolor = driver.findElement(By.xpath("//a[text()='Shop']")).getCssValu
e("background-color");
System.out.println(color);
System.out.println(backcolor);
Here if both color and backcolor different then that means that element is in di
fferent color.
60.
What is the selenium's recording language?
Ans- Not sure.
61.
How to get the title of the page?
Ans- getTitle();
62.
How do you get the width of the textbox?
Ans- Not sure.
63.
How do you get the attribute of the web element?
Ans- driver.getElement(By.tagName("img")).getAttribute("src") will get you the s
rc attribute of this tag.
Similarly you can get the values of attributes such as title, alt etc.
Similarly you can get CSS properties of any tag by using getCssValue("some prope
rty")
64.
How to check whether a text is underlined or not?
Ans- identify by getCssValue( border-bottom ) or sometime getCssValue( text-decoration )
method if the cssValue is there for that webElement or not.
This is for when moving cursor over element that is going to be underlined or no
tpublic class UnderLine {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://www.google.co.in/?gfe_rd=ctrl&ei=bXAwU8jYN4W
6iAf8zIDgDA&gws_rd=cr");
String cssValue= driver.findElement(By.xpath("//a[text()='Hindi'
]")).getCssValue("text-decoration");
System.out.println("value"+cssValue);
Actions act = new Actions(driver);
act.moveToElement(driver.findElement(By.xpath("//a[text()='Hindi
']"))).perform();
String cssValue1= driver.findElement(By.xpath("//a[text()='Hindi
']")).getCssValue("text-decoration");
System.out.println("value over"+cssValue1);
driver.close();
}
}
65.
How to change the URL on a webpage using selenium web driver?
Ans- driver.get( url1 );
driver.get( url2 );
66.
How to verify the presence of tooltips for a link?
Ans- webElement.getAttribute("title");
67.
How to hover the mouse on an element?
Ans- Actions act = new Actions(driver);
act.moveToElement(webelement).perform();
68.
What is the use of getOptions() method?
Ans- getOptions() is used to get the selected option from the dropdown list.
69.
What is the use of deSelectAll() method?
Ans- it is used to deselect all the options which have been selected from the dr
opdown list.
70.
Is WebElement an interface or a class?
Ans- Interface.
71.
FirefoxDriver is class or an interface and from where is it inherited?
Ans- FirefoxDriver is a class. It implements all the methods of WebDriver interf
ace.
72.
Which is the super interface of webdriver?
Ans- SearchContext.
73.
What is the difference b/w close() and quit()?
Ans- close() - it will close the browser where the control is.
quit()- it will close all the browsers opened by WebDriver.
74.
What is the difference b/w getWindowHandles() and getWindowHandle() ?
Ans- getWindowHandles()- is used to get the address of all the open browser whic
h we can store in Iterator<String> type.
getWindowHandle()- is used to get the address of the current browser where the c
ontrol is.
75.
What is the use of contextClick() ?
Ans- It is used to right click.
76.
How to perform double click using webdriver?
Ans- Actions act = new Actions(driver);
act.doubleClick(webelement).perform();
77.
What is the use of AutoIt?
Ans- Some times while doing testing with selenium, we get stuck by some interrup
tions like a window based pop up.
But selenium fails to handle this as it has support for only web based applicati
on.
To overcome this problem we need to use AutoIT along with selenium script.
AutoIT is a third party tool to handle window based applications. The scripting
language used is VBScript.
78.
How to type text in a new line inside a text area?
Ans- element.sendKeys( Sanjay_Line1.\n Sanjay_Line2 );
use // \n for new line.
79.
Ans- driver.switchTo().defaultContent();
80.
81.
What is Datadriven framework & Keyword Driven?
Ans- Datadriven framework- In this Framework , while Test case logic resides in
Test Scripts, the Test Data is separated and kept outside the Test Scripts.
Test Data is read from the external files (Excel File) and are loaded into the v
ariables inside the Test Script.
Variables are used both for Input values and for Verification values.
Keyword Driven- The Keyword-Driven or Table-Driven framework requires the develo
pment of data tables and keywords, independent of the test automation tool used
to execute them.
Tests can be designed with or without the Application. In a keyword-driven test,
the functionality of the application-under-test is documented
in a table as well as in step-by-step instructions for each test.
82.
How do you take screen shot?
Ans- File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
//now we can do anything with this screenshot
like copy this to any folderFileUtils.copyFile(srcFile,new File( folder name where u want to copy/file_name.pn
g ));
//FileUtils.copyFile(f, new File("d:/abc.jpg"));
83. testNG and how you could initiate your automation script..
Ans-TestNG is a unit Testing tool/Framework; We can run TestNG class directly or
through TestNG Suite(xml) we can also integrate with other tool like ANT, Maven
, Jenkins
84. how to connect to database using selenium, it's requirements for connection
and commands for connection.
Ans-using JDBC.
Example
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" ) ;
Connection conn = DriverManager.getConnection( "jdbc:mysql://server","un","pw
d" ) ;
Statement stmt = conn.createStatement() ;
ResultSet rs = stmt.executeQuery( "SELECT * FROM lk_ask_queries" ) ;
String res=rs.getString(1) - FIRST COLUMN
rs.close() ;
stmt.close() ;
conn.close()
85. what is advantages of using testng, why can't we run selenium code directly?
Ans- can have 3kinds of report, parallel execution, skip a particular tc, change
the order of execution, lots of annotaions, can execute without main method etc
..
It is not necessary to use testng.xml to run the selenium code. You can directly
run your selenium code.
In order to create a test suite and run seperate test cases, you need some frame
work which drive the automation.
Here testng.xml can be called as "driver" which drives several test cases automa
ted using selenium code.
86. have you worked on Jenkins? What is advantages of it? What if developer does
not use Jenkins, but testing team has been asked to use it?
Can you do so without developer creating the build in Jenkins, your automation c
ode can start?
Ans- Jenkins is a continues integration tool; we use it to start the framework
execution automatically as soon as the build is received from dev team;
If Dev team is not using Jenkins still testing team can use it where we can sche
dule the framework execution time or we can write customized script which can tr
igger the Frame Work
87. disadvantages and advantages of selenium, where can all I not use selenium?
AnsDisadvantages- can't automate windows based application, Cant Handle Tab Browser
es,Cant Handle Existing Browser; Cant Hanled Flex/Flash application etc.
Advantages- Free, Open Source, Platform independed and Supports All Browsers
88. what is the code to find the active element in the web page?
Ans- List<WebElement> allElements = driver.findElements(By.xpath("//*"));
for(int i=0;i<allElements.size();i++)
{
WebElement e=allElements.get(i);
if(e.isEnabled())
{
System.out.println(e.getTagName());
}
}
90. write a selenium program to verify that the navigation of the web page is la
nding at the correct URL at the next page..
Ans- driver.fiindElement(By.xpath( xpath of anylink which u wan to check )).click();
String expURL = give expected url ;
String act = driver.getCurrentUrl();
Assert.assertEquals(act,exp);
91. write a code to make use of assert if my username is incorrect.
Ans- try
{
Assert.assertEquals(actUserName),expUserName)
}
catch(Exception e)
{
Syste.out.println( name is invalid );
}
92. what is the skeleton of testng.xml? What all does/can it contain?
Anssuite
|- test
|-classes
|- class
93. code to write the usage of actions class with an example, usage of select cl
ass
Ans-
TimeoutException
SessionNotFoundException
ElementNotVisibleException
ElementNotSelectableException
NoAlertPresentException
NoSuchAttributeException
NoSuchWindowException
WebDriverException
106. Suppose we have multiple tabs like in TestNG(Failed Tests,Run Last-test etc
..)how will u handle it?
if a tab is inside a page then it will be like clicking on Link, but web driver
cant handle tabbed browser
107. In a drop-down we have many options out of which i want to write xpath for
particular option,How will u write?
//select[@name='somename]/options[1]
108. We have two similar hidden elements with same attribute how can u write xpa
th?
we should use style attribute
contains(@style,'display: none') --> for invisible elements
contains(@style,'display: block') --> for visible elements
109. How to handle untrusted connection in selenium-2?
it is automatically suppressed in Mozilla, for other we can use java script
110. Using AND,OR operation how can u write xpath for dynamic elements?
//a[text()>6 and text()<9]
//a[text()>6 or text()<9]
111. How many test cases u automate per day?
4 to 5
112. I have a server message(Report generated successfully) but i need only repo
rt,how u write the script?
String sMsg=driver.findElement(By.id()).getText();
System.out.println(sMsg.split(" ")[0]);
113. Why u call it as IDE?What actually mean IDE?
Its Integrated development environment
because it conatins Editor,Debugger,Intelli-sense coding features
114. How to handle Default Browser in Selenium-2?
you mean opening Browser with default profile?
ProfilesIni prof = new ProfilesIni();
FirefoxProfile p = prof.getProfile("default");
WebDriver driver = new FirefoxDriver(p);
115. What is bitmap Comparison?why we use it?
use to compare expected and actual images
116. will we use regular expressions?if so why?if not why?
we can use * in xpath for dynamic element
117. How do u compare bitmap in selenium webdriver?
We use "TakesScreenshot" and ImageIO class to comapare bitmap
118. What is Agile method?
its a type of SDLC and it is based on iterative and incremental development