August 27, 2019

Srikaanth

Virtusa Selenium Recently Asked Interview Questions

What are the different types of driver implementation?

AndroidDriver, AndroidWebDriver, ChromeDriver, EventFiringWebDriver, FirefoxDriver, HtmlUnitDriver, InternetExplorerDriver, IPhoneDriver, IPhoneSimulatorDriver, RemoteWebDriver, SafariDriver, WebDriverBackedSelenium

Code for Opening Firefox browser?

Webdriver driver=new FireFoxdriver();

Which repository you have used to store the test scripts?

I have created scripts in excel file and store them in Test cases folder under src .

How to work with radio button in web driver?

We can select the value from the drop down by using 3 methods.

selectByVisibleText – select by the text displayed in drop down
selectByIndex – select by index of option in drop down
selectByValue – select by value of option in drop down

WebElement e = driver.findElement(By.id(“44”));
Select selectElement=new Select(e);
// both of the below statements will select first option in the weblist
selectElement.selectByVisibleText(“xyz”);
selectElement.selectByValue(“1”);
Virtusa Selenium Recently Asked Interview Questions Answers
Virtusa Selenium Recently Asked Interview Questions Answers

How to work with dynamic web table?

You can get the total number of

tags within a

tag by giving the xpath of the

element by using this function –
List ele = driver.findElements(By.xpath(“Xpath of the table”));
Now you can use a for each loop to loop through each of the tags in the above list and then read each value by using getText() method.

Detail about TestNG Test Output folder.

It is the directory where reports are generated. Every time tests run in a suite, TestNG
creates index.html and other files in the output directory.

In frame if no frame Id as well as no frame name then which attribute I should consider throughout our script.

You can go like this…..driver.findElements(By.xpath(“//iframe”))…
Then it will return List of frames then switch to each and every frame and search for the locator which you want then break the loop

What is object repository?

It is collection of object names their properties, attributes and their values .It maye be excel, XML,property file or text file

TestNG vs. Junit?

Advantages of TestNG over Junit

In Junit we have to declare @BeforeClass and @AfterClass which is a constraint where as in TestNG there is no constraint like this.
Additional Levels of setUp/tearDown level are available in TestNG like @Before/AfterSuite,@Before/AfterTest and @Before/AfterGroup
No Need to extend any class in TestNG.
There is no method name constraint in TestNG as in Junit. You can give any name to the test methods in TestNG
In TestNG we can tell the test that one method is dependent on another method where as in Junit this is not possible. In Junit each test is independent of another test.
Grouping of testcases is available in TestNG where as the same is not available in Junit.
Execution can be done based on Groups. For ex. If you have defined many cases and segregated them by defining 2 groups as Sanity and Regression. Then if you only want to execute the “Sanity” cases then just tell TestNG to execute the “Sanity” and TestNG will automatically execute the cases belonging to the “Sanity” group.
Also using TestNG your selenium test case execution can be done in parallel.

What is the difference between @beforemethod and @beforeclass.

In JUnit4 @Before is used to execute set of preconditions before executing a test.

For example, if there is a need to open some application and create a user before
executing a test, then this annotation can be used for that method. Method that is
marked with @Before will be executed before executing every test in the class.

If a JUnit test case class contains lot of tests which all together need a method
which sets up a precondition and that needs to be executed before executing the
Test Case class then we can utilise “@BeforeClass” annotation.

What are the different Parameters for @Test annotation?

Parameters are keywords that modify the annotation’s function.

Can we run group of test cases using TestNG?

Test cases in group in Selenium using TestNG will be executed with the below options.
If you want to execute the test cases based on one of the group like regression test or smoke test
@Test(groups = {“regressiontest”, “smoketest”})

When will you get element not clickable exception in Selenium?

The reason for the element is not clickable at point(x,y) exception.

      Some of my observation was

It mostly happens in Chrome so if you are mostly working with Firefox or IE then you will not be getting this exception.
Chrome does not calculate the exact location of element
Chrome always click in the middle of Element.
Sometimes you will get this exception due to Sync issue also.

How to solve Not connected Exception – unable to connect to host in selenium webdriver?

Scripts that worked earlier may be till yesterday are NOW not working because of Firefox browser upgraded to new version.

      Most of them have faced the similar problem when the browser has updated to version. This is the first issue user notices when there is an update in the Firefox browser and may not support selenium with the older version of jars.

      If you already have the latest version of selenium, then you have to degrade your browser until there is an update from selenium.

      Problem :

      Webdriver will just initiate the browser, prompting for a search or address and that ends there with an exception:

      org.openqa.selenium.firefox.NotConnectedException:

What is Selenium?

Selenium is a Suite (group) of tools i.e., Selenium IDE, Selenium WebDriver and Selenium Grid to automate web browsers across many platforms.

What is Selenium IDE, WebDriver, and Grid?

Selenium IDE: It is a Firefox plugin which is used to Record and Play the Test Scripts.

Selenium WebDriver: It is a tool which provides an API (that can be understood easily) to automate web browsers with the help of programming languages like Java, Csharp, Python, PHP, Perl, Ruby, and JavaScript.

Selenium Grid: It transparently distributes tests into Remote machines. That is, running multiple tests at the same time against different machines running different browsers and operating systems.

What are the locators available in WebDriver?

Locators: Locators are used to identifying or locate an element (text box, radio buttons, links, check boxes, drop downs, images, text etc. ) in the web page.

Webdriver supports 8 locators i.e., Id, Name, Class Name, Link Text, Partial Link Text, XPath, CSS and Tag Name.

Note: Id is the fastest locator among this 8 locators.

How to launch Firefox, Chrome, IE browser using WebDriver?

The following syntax can be used to launch Browser:

WebDriver driver = new FirefoxDriver();

WebDriver driver = new ChromeDriver();

WebDriver driver = new InternetExplorerDriver();

How to set System Property in WebDriver?

We can set the system property by using setProperty() method, which is a static method defined in System class for which we need to pass 2 parameters i.e., driver name and path of the executable file of the driver.

For Example Setting system property to launch chrome browser

public class ChromeBrowser {

WebDriver driver;

public static void main(String[] args) {

System.setProperty(“webdriver.chrome.driver”, “E://chromedriver.exe”);

driver = new ChromeDriver();

driver.get(“http://www.google.com”);

}

}

How to find font color of Text element?

By using getCssvalue()

For example:

driver.findElement(By.id(“text”)).getCssValue(“color”);

How to find size of the Image?

By using getSize()

How to find Dynamic Web Elements?

We can Dynamic web elements with the help of XPath or CSS.

For Example:

By using XPath:

driver.findElement(By.xpath(“//div[contains(@id,’yui_’)]”));

By using CSS:

driver.findElement(By.cssSelector(“div[id*=’yui_’)]”));

How to skip a test case in JUnit?

By using @Ignore annotation

@Ignore

@Test

public void testDemo(){

System.out.println(“This is testDemo Method”);

}

@Test

public void testPractice(){

System.out.println(“This is testPractice Method”);

}

Output:

This is testPractice Method

Note: Execution in JUnit is carried out in Alphabetical Order

Which is more preferable xpath or CSS?

CSS Selector

How  to find out all the links in webpage?

List<WebElement> allLinks = driver.findElements(By.tagName(“a”));

How we will configure the Parallel browser testing in testng.xml?

<suite name=”Parallel test suite” parallel=”tests”>

Explain about TestNG – its listeners?

      ITestListener has following methods

OnStart – OnStart method is called when any Test starts.
onTestSuccess– onTestSuccess method is called on the success of any Test.
on test failure– on test failure method is called on the failure of any Test.
onTestSkipped – onTestSkipped method is called on skipped of any Test.
onTestFailedButWithinSuccessPercentage – method is called each time Test fails but is within success percentage.
onFinish – onFinish method is called after all Tests are executed

Write a Syntax to take the screenshot?

File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

    //The below method will save the screen shot in d drive with name “screenshot.png” 

    FileUtils.copyFile(scrFile, new File(“D:\\screenshot.png”));

How to find out all the frames in a webpage?

List<WebElement> ele = driver.findElements(By.tagName(“frame”));

     System.out.println(“Number of frames in a page :” + ele.size());

How to handle cookies?

In Selenium Webdriver interact with cookies with below built-in methods

     driver.manage().getCookies();   // Return The List of all Cookies

     driver.manage().getCookieNamed(arg0);  //Return specific cookie according to name

     driver.manage().addCookie(arg0);   //Create and add the cookie

     driver.manage().deleteCookie(arg0);  // Delete specific cookie

     driver.manage().deleteCookieNamed(arg0); // Delete specific cookie according Name

     driver.manage().deleteAllCookies();  // Delete all cookies

What is Selenium Grid?

      Selenium Grid is a tool used together with Selenium RC to run parallel tests across different machines and different browsers all at the same time. Parallel execution means running multiple tests at once.

Features:

Enables simultaneous running of tests in multiple browsers and environments.
Saves time enormously.
Utilizes the hub-and-nodes concept. The hub acts as a central source of Selenium commands to each node connected to it.

Difference between find elements and find element?

     FINDELEMENT() METHOD:

FindElement method is used to access a single web element on a page. It returns the first matching element. It throws a NoSuchElementException exception when it fails to find If the       element.

     Syntax:

     driver.findElement(By.xpath(“Value of Xpath”));

FINDELEMENTS() METHOD:

FindElements method returns the list of all matching elements. The findElement method throws a NoSuchElementException exception when the element is not available on the page.           Whereas, the findElements method returns  an empty list when the element is not available or doesn’t exist on the page. It doesn’t throw NoSuchElementException.

     Syntax:

     List link = driver.findElements(By.xpath(“Value of Xpath”));

Explain about softAssert and HardAssert?

Asserts are used to perform validations in the test scripts.

There are two types of Assert:

Hard Assert
Soft Assert
When an assert fails the test script stops execution unless handled in some form. We call general assert as Hard Assert.

      Hard Assert – Hard Assert throws an AssertExceptionimmediately when an assert statement fails and test suite continues with next @Test.

The disadvantage of Hard Assert – It marks method as fail if assert condition gets failed and the remaining statements inside the method will be aborted.

       To overcome this we need to use Soft Assert. Let’s see what is Soft Assert.

       Soft Assert – Soft Assert collects errors during @Test. Soft Assert does not throw an exception when an assert fails and would continue with the next step after the assert statement.

       If there is any exception and you want to throw it then you need to use assertAll() method as a last statement in the @Test and test suite again continue with next @Test as it is.

       We need to create an object to use Soft Assert which is not needed in Hard Assert.


Subscribe to get more Posts :