Selenium Questions Answers
Selenium Questions Answers
<html>
<body>
<div>
<span>text1</span>
<span>text2</span>
<span>text3</span>
<span>text100</span>
</div>
</body>
</html>
Text(): In this expression, with text function, we find the element with exact text
match as shown above. In our case, we find the element with text "text3".
Xpath=//span[text()='text3']
2)how would you handle exception in your project specially when element is not
found , not visible and when browser suddently closed?
We handle such conditions and then prints a user friendly warning message to user
through try/catch.
Example:
try {
driver.findElement(By.name(nameValue)).clear();
driver.findElement(By.name(nameValue)).sendKeys(data);
System.out.println("The data: "+data+" entered
successfully in field :");
bReturn = true;
}
catch (NoSuchElementException e) {
}
catch (NoSuchSessionException e) {
In testng.xml file we can specify the specific package name , which needs to be
executed.
In a project there may be many packages, but we want to execute only the selected
packages.
We need to specify the names of the packages in between the package tags.
Example:
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
4)How to run method in paraller using TestNG xml from single class file?
The method can be run in paraller using Method groups.You can also exclude or
include individual methods:
<suite name="My suite" parallel="methods" thread-count="2">
<test name="Test1">
<classes>
<class name="example1.Test1">
<methods>
<include name="TestMethod1"/>
<include name="TestMethod2"/>
</methods>
</class>
</classes>
</test>
TestNG will run all your test methods in separate threads. Dependent methods will
also run in separate threads but they will respect the order that you specified.
5)Annotations:
@BeforeSuite: The annotated method will be run before all tests in this suite have
run.
@AfterSuite: The annotated method will be run after all tests in this suite have
run.
@BeforeTest: The annotated method will be run before any test method belonging to
the classes inside the <test> tag is run.
@AfterTest: The annotated method will be run after all the test methods belonging
to the classes inside the <test> tag have run.
@BeforeGroups: The list of groups that this configuration method will run before.
This method is guaranteed to run shortly before the first test method that belongs
to any of these groups is invoked.
@AfterGroups: The list of groups that this configuration method will run after.
This method is guaranteed to run shortly after the last test method that belongs to
any of these groups is invoked.
@BeforeClass: The annotated method will be run before the first test method in the
current class is invoked.
@AfterClass: The annotated method will be run after all the test methods in the
current class have been run.
@BeforeMethod: The annotated method will be run before each test method.
@AfterMethod: The annotated method will be run after each test method.
Attributes:
alwaysRun ->If set to true, this test method will always be run even if it depends
on a method that failed.
dataProvider ->The name of the data provider for this test method.
dataProviderClass ->The class where to look for the data provider. If not
specified, the data provider will be looked on the class of the current test method
or one of its base classes. If this attribute is specified, the data provider
method needs to be static on the specified class.
priority ->The priority for this test method. Lower priorities will be scheduled
first.
singleThreaded ->If set to true, all the methods on this test class are guaranteed
to run in the same thread, even if the tests are currently being run with
parallel="methods". This attribute can only be used at the class level and it will
be ignored if used at the method level. Note: this attribute used to be called
sequential (now deprecated).
Parameters : The Parameters is used, when you need to pass a static data for your
test cases. which remains unchanged throughout the execution of test cases. Which
is purely implemented in XML file.
Ex: <parameter name="locator" value=""></parameter>
Data Provider: The Data Provider is used, when you need to pass a Dynamic data for
your test cases. which will changes for each iteration of a test cases.
Ex: @DataProvider(name="value")
Ans: We can execute failed test cases using TestNG in Selenium, By using �testng-
failed.xml�
->After the first run of an automated test run. Right click on Project and Click on
Refresh.
->A folder will be generated named �test-output� folder. Inside test-output folder,
you could find �testng-failed.xml�
->Run �testng-failed.xml� to execute the failed test cases again.
8)A class has 4 methods a, b, c, d. Here the method 'b' depends on 'c' and method
'c' depends on 'a'. Then what will be the order of execution?
Ans:All independent methods will execute first and followed by depend methods.
The order of execution is method 'a' -> method 'd' -> method 'c' and 'b'.
9) Given that you have 250 test cases but you do not want to run 150 of them
unless the other 100 is successful . How do you achieve them in testng?
Make first 100 test cases as a single group [example: @test(groups={"smoke"})],
then make next 150 test cases another group [example: : @test(groups={"sanity"})]
and also make it depends on previous group (example: [@test(groups="sanity",
dependsongroups ="smoke")].
This will force testcases 101 - 250 to run only after first 100 test cases run
successfully.
10)Is it possible to run testcases in dataprovider in parallel for each test data
given? If yes ,how to do that? ? If no , why can�t it be done?
@dataprovider(parallel = true)
Parallel data providers running from an xml file share the same pool of threads,
which has a size of 10 by default.
You can modify this value in the <suite> tag of your xml file: