0% found this document useful (0 votes)
4 views5 pages

Selenium Questions Answers

Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
0% found this document useful (0 votes)
4 views5 pages

Selenium Questions Answers

Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1/ 5

1)Consider one div having 100 spans with uniaue text.

How to find expected text


using XPath?

<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) {

System.out.println("The Element not found");


}
catch (ElementNotVisibleException e) {

System.out.println("The Element not Visible");

}
catch (NoSuchSessionException e) {

System.out.println("The Browser is closed");

3)What is the use of <package> tag in TestNG xml ?

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" >

<suite name="Suite1" verbose="5" >


<test name="Regression1" >
<packages>
<package name="test.sample" />
</packages>
</test>
</suite>
TestNG will look at all the classes in the package test.sample and will retain only
classes that have TestNG annotations.

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.

dependsOnGroups ->The list of groups this method depends on.

dependsOnMethods ->The list of methods this method depends on.

description ->The description for this method.

enabled ->Whether methods on this class/method are enabled.

expectedExceptions ->The list of exceptions that a test method is expected to


throw. If no exception or a different than one on this list is thrown, this test
will be marked a failure.

groups ->The list of groups this class/method belongs to.


invocationCount ->The number of times this method should be invoked.
invocationTimeOut ->The maximum number of milliseconds this test should take for
the cumulated time of all the invocationcounts. This attribute will be ignored if
invocationCount is not specified.

priority ->The priority for this test method. Lower priorities will be scheduled
first.

successPercentage ->The percentage of success expected from this method

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).

timeOut ->The maximum number of milliseconds this test should take.


threadPoolSize ->The size of the thread pool for this method. The method will be
invoked from multiple threads as specified by invocationCount.
Note: this attribute is ignored if invocationCount is not specified.

6)Difference between parameters and dataproviders?

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")

7)How do you run failed test cases using TestNG?

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?

Data providers can run in parallel with the attribute parallel:

@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:

<suite name="suite1" data-provider-thread-count="20" >


If you want to run a few specific data providers in a different thread pool, you
need to run them from a different xml file.

You might also like