21,Explain About TestNG:

TestNG is a framework similar to Junit. 

Advantages of TestNG:

1. More and detailed annotations.

2. Paralllel execution.

3. Test Groups (Grouping scripts into groups like sanity/smoke/regression and executing group of tests).

Annotations and their Heirarchy inTestNG:

@BeforeSuite ->@BeforeTest -> @BeforeClass -> @BeforeMethod -> @Test -> @AfterMethod ->@AfterClass ->@AfterTest -> @AfterSuite

@Test Attributes:

  • Invocation Count: To run the script multiple times. (example: @Test (invocation count =2)
  • Enabled: To run/skip a script. (Example: @Test(enabled = true) ) [By default the enabled will be set as 
  • true. If you set that as false that script will be skipped.]
  • Timeout: To set timeout for a script.
  • Groups: To group the scripts into a group. (example: @Test(Groups =”regression”)
  • Priority: To set priority to the execution order of @Test (Scripts) . (example: @Test (Priority =1) 

Priority takes the order of least value to a higher value. If two (@Test have priority 2 and 5 respectively: @Test with priority 2 will be executed first).

Note: Also take a look at other attributes like DependsonGroups, DependsonMethods.


A. Explain the Hierarchy of TestNG Annotations.

1. Suppose if I have 3 @Test in my script what is their execution order.

Ans: The @Test scripts will be executed alphabetically)

2. If I want to run the 3rd @Test first, how to achieve that?

Ans: Use the @Test attribute “priority”)

3. How many times @BeforeSuite, @BeforeTest and @BeforeMethod will be executed?

Ans: @ BeforeSuite will be executed only once.

 @BeforeMethod will be executed before each method within @Test annotation.

 @BeforeTest will be executed before each “test tag” – (<test>) in the TestNG XML file. 

B. Parall Execution:

In the above code if we change the first line of code as:

<Suite name=”Default Suite” parallel =”Classes”>

The classes TC001 and TC002 will be executed parallel.


22,Challenges faced in Automation:

Important Interview question: Tell any 2

1. Screenshots taken in browsers other than “Firefox” will not cover the entire webpage. Only the area with the element of focus will be covered. Solution: Use Actions class to move (scroll) the webpage up/down and take screenshots.

2. Dynamically changing xpath: Solution: Used partial xpath to handle it.

3. Need to start scripting before the application is ready. That is we have mockup screen alone. Solution: Using Properties file concept and handled it. Once the application is ready we will simply change the code in the properties file rather than changing script.

4. Need to upload a document from my computer as part of sanity testing which would need windows application interactions. We can achieve it using tools like Sikuli or Autoit. But in client machines we should not install such tools. Solution: There is a method “Robot.awt” under “Java.AWT” package. *Concept: store the file path using the “setContents” method to store in clipboard. Then using sendKeys method send “Ctrl+V” and “Enter.”

5. Reading contents from a PDF file. Solution: Two jar files – “PDF Box” to read PDF and “Font box” to read/analyze” the contents in the PDF. (Limitation: It cannot read images/tables. Only text will be read.)

6. Run execution in the background or Hidden mode. Solution: HTML Unit driver is used to run scripts in the background. That is we don’t want any browser to run in the foreground. [Limitation of HTML unit driver: it cannot take screenshot. Mouse hover, Key board actions cannot be performed].


23,How you will move from one window to another? 

First we will check what all windows are open by using driver.getwindowhandles, to get set of opened windows , then I use iterator to iterate over each of the pages and inside for loop will check like Current URL matches with the excepted page, if match then switch to that window by using driver.switchTo(Destination window) -> to return back to main parent window use driver.defaultwindow. 


24,Tell me the difference between Implicit & Explicit wait?

Implicit wait applies for all the elements and all the tests like if we give 10 sec of implicit wait it will wait for 10 sec for each element before giving nosuchelement exceptions. While Explicit wait can be applied for any particular step for which you want extra wait time so we can use explicit wait. We can use mix of both waits to depend on the situation of the step.


 25,Can you tell me some exceptions in selenium?

 NoSuchElementException, NoSuchWindowException NoSuchframeException, StaleElementReferenceException, TimeoutException.


26,Can you tell me about StaleElementReferenceException? 

Stale means old or decayed, here it sounds like element which was present on that page is no longer there or decayed. To handle this, we can refresh the webpage before pointing to that element. We can write script for waiting via explicit wait by writing expected condition.refresh. Or we can go with page object model in that we can over come this stale element exception.


27,What do you mean by User Defined Exception? 

User Defined Exception or custom exception is creating your own exception class and throws that exception using 'throw' keyword. This can be done by extending the class Exception. ... The keyword “throw” is used to create a new Exception and throw it to the catch block. 


28,Can you tell me what is assert in TestNG? 

Assert is like verification where we check like expected thing and actual thing are same or not. 


29,Which assert you have used in TestNg? 

We have used Hard assert and Soft assert, while applying Hard assert if we found any glitch in expected and actual then it will through exception and move to next @test while Soft assert it won’t give exception and move to next step of that test. And to get all the exceptions in console we need to write at the end assert.all.


30,Can you tell me about the order of TestNG annotations?

@BeforeSuite @BeforeTest @BeforeClass @BeforeMethod @Test @AfterMethod @AfterClass @AfterTest @AfterSuite 


31,Do you heard about Priority in TestNg can we set -ve priority? 

Yes, like priority is 0, -1, TestNg will run -1 then 0 then 1. And if we have any @test which is not having any priority set, then in that case it will search via alphabetic order whichever comes first and execute test respectively.


32, Do you work in cucumber, can you tell me what all files required in cucumber?

In cucumber we have Feature file, Step Definition file and Test Runner file. In feature file we used to write scenario in gherkin language which is most like in plain English language. Here we use some of the keywords like feature, scenario, scenario outline, given, when, then, and, example, background keywords for writing our test scenarios steps. In Step Definition file we write mapping code for all the scenario of feature file. In test Runner file we provide the address of the feature file, step definition file, and all-important Tags, Plugin, Listeners in that.


33,What is the difference between scenario & scenario outline?

When we have single scenario and we need to run it one time at that place we use Scenario. If you want some parametrization or Data Driven testing at that time, we can use scenario outline where we have to use Example keyword like if we are running this scenario for 3 different data set like username & pass. so, it will run 3 times.


34,Can you tell me more about Background Keyword? 

Background is used when we have some common Given part. Suppose we have pre-condition that we have to check this before each scenario. so in order to avoid rewriting same step we can write it in Background.


35,What is the use of Dry Run in cucumber?

 Dry run is not running our whole application it will check whether all features are mapped with Step definition. 


36,What is hooks in cucumber?

 In cucumber we use hooks for common functionalities, hooks are like we want to run before & after each of the scenario. In hooks we have 2 different @before, @ after which run before and after of each scenario. Also @beforestep, @afterstep which run before and after each step.


37,Can you tell me how you will re-run failed scenario in cucumber? 

For that we can use re-run attribute in our test runner file. After that we can write one file location. Where all the test cases which failed while execution get stored. So next time while running execution we can give this file location and run the failed TC. 


38,You have worked in Cucumber & TestNG according to you which one is best? 

I will consider Cucumber as it is most likely understood by Laymen people which is English plain language. Because in order to understand the functionality flow no need to go look and script/code. Via Scenario steps lines only we can get clear understanding about the functionality. It helps to come all the QA members Dev, Client, Product Owner on same page.


39,Can you explain me TestNG?

 TestNG is advanced version of Junit only. It is mainly used by Dev/QA for maintain the code easily and for unit testing. It provides lots of benefits to us like we can create a suite and we can write all the required Tc in one go only using that suite. We can group our Tc we can set priority we can run our tc in parallel mode, We can generate good reports via TestNG. We can write functionality depends on methods, depends on group. We can run single tc multiple time with single set of data of multiple set of Data.


40,How to run single method multiple time in TestNG?

 We have invocation count attribute in @test annotiation. We can write invocation count as 3 if we want to run it 3 times. Apart from that we can write threadpull.size if we want to run that case in multiple thread.

Post a Comment

Previous Post Next Post