Syntel Selenium Recently Asked Interview Questions Answers
What is the difference between driver.close() and driver.quit() command?
close(): This method is used to close the current active window of the browser.
quit(): This method is used to close all the associated windows of the browser that are opened.
How to handle Javascript Alerts or web-based pop-ups?
To handle Javascript Alerts, WebDriver provides an Interface called Alert.
First, we need to switch the driver focus to alerts.
Syntax:
Alert alert=driver.switchTo().alerts();
alert.getText() – The getText() method retrieves the text displayed on the alert box.
alert.accept() – The accept() method clicks on the “Ok” button as soon as the pop-up window appears.
alert.dismiss() – The accept() method clicks on the “Cancel” button as soon as the pop-up window appears.
alert.sendKeys(String stringToSend) – The sendKeys() method enters the specified string pattern into the alert box.
How to capture screenshot in WebDriver?
public String getScreenshot() throws Exception {
try {
driver.get(“https://www.google.co.in/”);
driver.findElement(By.linkText(“Gmail”)).click();
return “Pass”;
}
catch(Exception e) {
DateFormat dateFormat = new SimpleDateFormat(“dd-MM-yyyy HH-mm-ss”);
Date dt = new Date();
File screenshotFile = ((TakesScreenshot) d).getScreenshotAs (OutputType.FILE);
FileUtils.copyFile(screenshotFile, new File(“F:\\Selenium_Scripts_Feb17\\Results\\”+dateFormat.format(dt)+”.png”));
return “Fail”;
}
}
Write a code to login into any site showing uthentication pop-up for username and password?
//Pass Username and Password in URL itself
driver.get(“http://username:password@www.xyz.com”);
How do you send ENTER key?
driver.findElement(By.id(“email”)).sendKeys(Keys.ENTER);
How do you access Firefox profiles in WebDriver?
//create object of ProfilesIni class which holds all the profiles of FF
ProfilesIni pr=new ProfilesIni():
//To load the profile
FirefoxProfile fp=pr.get(“user”);
WebDriver driver=new FirefoxDriver(fp);
driver.get(“http://google.com”);
What are the different types of Waits or Synchrinization commands in WebDriver?
Usually, the time delay between two webdriver elements is 0 milliseconds but the application speed varies based on different factors:
Internet speed
Server response time
System configuration
To avoid the timing errors(Synchronization errors) between the tool (webdriver) and application, we take the help of Synchronization commands.
Implicit Wait: It is used to define wait for all the elements of WebDriver.
Syntax:
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
Explicit Wait: It is used to define wait for specific element of WebDriver.
Syntax:
WebDriverWait wait=new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(By.linkText(“Gmail”)));
Write a syntax to scroll down a page in selenium?
JavascriptExecutor jsx=((JavascriptExecutor) driver);
jsx.executeScript(“window.scrollBy(0,500)”,””);
How to check if text is present on a web page?
String text=”Selenium”;
boolean isTextPresent=(driver.getPageSource()).contains(text);
System.out.println(text+” is present in the web page”);
What are the different exceptions in Selenium web driver?
The different exceptions in Selenium web drivers are
WebDriverException
ElementNotVisibleException
NoAlertPresentException
NoSuchWindowException
NoSuchElementException
TimeoutException
StaleElementReferenceException
How to handle MouseHover?
By using moveToElement() method in Actions class
//Create an object to Actions class
Actions action = new Actions(driver);
action.moveToElement(element);
How to handle Frames?
driver.switchTo().frames(id/name/index/loactor);
How to switch from child frame to parent frame?
To switch back from child frame to parent frame use method parentFrame()
Syntax:
driver.switchTo().parentFrame();
Explain how to switch back from a frame?
To switch back from a frame use method defaultContent()
Syntax:
driver.switchTo().defaultContent();
What is the difference between “/” and “//”
Single Slash “/” – Single slash is used to create XPath with absolute path i.e. it locates the element from the root node.
Example:
/html/body/div[2]/div[4]/table/tbody/tr[4]/td/span
Double Slash “//” – Double slash is used to create XPath with relative path i.e. it locates the element from the current node.
Example:
//input[@name=’email’]
What is the difference between Assert and Verify in Selenium?
There is no difference between Assert and Verify in a positive scenario.
Assert: If the assert condition is true then the program control will execute the next test step but if the condition is false, the execution will stop and further test step will not be executed.
Verify: If the verify condition is true then the program control will execute the next test step but if the condition is false, the execution of the current step will be skipped and further test step will be executed.
What is the super interface of WebDriver?
SearchContext is the super interface of WebDriver
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”);
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”})
In what all case we have to go for “JavaScript executor”.
Consider FB main page after you login. When u scrolls down, the updates get loaded. To
handle this activity, there is no selenium command. So you can go for javascript to set
the scroll down value like driver.executeScript(“window.scrollBy(0,200)”, “”);
Selenium supports UI and functional testing. As well it can support performance testing
for reasonable load using selenium grid.
Assertions are like Assessors, but they verify that the state of the application conforms to what is expected. Examples include “make sure the page title is X” and “verify that this check box is checked”.
Assertions are like Accessors, but they verify that the state of the application conforms to what is expected. Examples include “make sure the page title is X” and “verify that this checkbox is checked”.
All Selenium Assertions can be used in 3 modes: “assert”, “verify”, and “waitFor”.
For example, you can “assertText”, “verifyText” and “waitForText”. When an “assert” fails, the test is aborted. When a “verify” fails, the test will continue execution, logging the failure. This allows a single “assert” to ensure that the application is on the correct page, followed by a bunch of “verify” assertions to test form field values, labels, etc.
“waitFor” commands wait for some condition to become true (which can be useful for testing Ajax applications). They will succeed immediately if the condition is already true. However, they will fail and halt the test if the condition does not become true within the current timeout setting (see the setTimeout action below).
How to store a value which is text box using web driver?
driver.findElement(By.id(“your Textbox”)).sendKeys(“your keyword”);
How to handle alerts and confirmation boxes.
Confirmation boxes and Alerts are handled in same way in selenium.
var alert = driver.switchTo().alert();
alert.dismiss(); //Click Cancel or Close window operation
alert.accept(); //Click OK
Handle Confirmation boxes via JavaScript,
driver.executeScript(“window.confirm = function(message){return true;};”);
How to mouse hover on an element?
Actions action = new Actions(webdriver);
WebElement we = webdriver.findElement(By.xpath(“html/body/div[13]/ul/li[4]/a”));
action.moveToElement(we).moveToElement(webdriver.findElement(By.xpath(“/expression-here”))).click().build().perform();
How to switch between the windows?
private void handlingMultipleWindows(String windowTitle) {
Set windows = driver.getWindowHandles();
for (String window : windows) {
driver.switchTo().window(window);
if (driver.getTitle().contains(windowTitle)) { return; } } }
How to switch between frames?
WebDriver’s driver.switchTo().frame() method takes one of the three possible arguments:
A number.
Select a frame by its (zero-based) index. That is, if a page has three frames, the first frame would be at index “0”, the second at index “1” and the third at index “2”. Once the frame has been selected, all subsequent calls on the WebDriver interface are made to that frame.
A name or ID.
Select a frame by its name or ID. Frames located by matching name attributes are always given precedence over those matched by ID.
A previously found WebElement.
Select a frame using its previously located WebElement. Get the frame by it’s id/name or locate it by driver.findElement() and you’ll be good.
What is the difference between driver.close() and driver.quit() command?
close(): This method is used to close the current active window of the browser.
quit(): This method is used to close all the associated windows of the browser that are opened.
How to handle Javascript Alerts or web-based pop-ups?
To handle Javascript Alerts, WebDriver provides an Interface called Alert.
First, we need to switch the driver focus to alerts.
Syntax:
Alert alert=driver.switchTo().alerts();
alert.getText() – The getText() method retrieves the text displayed on the alert box.
alert.accept() – The accept() method clicks on the “Ok” button as soon as the pop-up window appears.
alert.dismiss() – The accept() method clicks on the “Cancel” button as soon as the pop-up window appears.
alert.sendKeys(String stringToSend) – The sendKeys() method enters the specified string pattern into the alert box.
Syntel Selenium Recently Asked Interview Questions Answers |
How to capture screenshot in WebDriver?
public String getScreenshot() throws Exception {
try {
driver.get(“https://www.google.co.in/”);
driver.findElement(By.linkText(“Gmail”)).click();
return “Pass”;
}
catch(Exception e) {
DateFormat dateFormat = new SimpleDateFormat(“dd-MM-yyyy HH-mm-ss”);
Date dt = new Date();
File screenshotFile = ((TakesScreenshot) d).getScreenshotAs (OutputType.FILE);
FileUtils.copyFile(screenshotFile, new File(“F:\\Selenium_Scripts_Feb17\\Results\\”+dateFormat.format(dt)+”.png”));
return “Fail”;
}
}
Write a code to login into any site showing uthentication pop-up for username and password?
//Pass Username and Password in URL itself
driver.get(“http://username:password@www.xyz.com”);
How do you send ENTER key?
driver.findElement(By.id(“email”)).sendKeys(Keys.ENTER);
How do you access Firefox profiles in WebDriver?
//create object of ProfilesIni class which holds all the profiles of FF
ProfilesIni pr=new ProfilesIni():
//To load the profile
FirefoxProfile fp=pr.get(“user”);
WebDriver driver=new FirefoxDriver(fp);
driver.get(“http://google.com”);
What are the different types of Waits or Synchrinization commands in WebDriver?
Usually, the time delay between two webdriver elements is 0 milliseconds but the application speed varies based on different factors:
Internet speed
Server response time
System configuration
To avoid the timing errors(Synchronization errors) between the tool (webdriver) and application, we take the help of Synchronization commands.
Implicit Wait: It is used to define wait for all the elements of WebDriver.
Syntax:
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
Explicit Wait: It is used to define wait for specific element of WebDriver.
Syntax:
WebDriverWait wait=new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(By.linkText(“Gmail”)));
Write a syntax to scroll down a page in selenium?
JavascriptExecutor jsx=((JavascriptExecutor) driver);
jsx.executeScript(“window.scrollBy(0,500)”,””);
How to check if text is present on a web page?
String text=”Selenium”;
boolean isTextPresent=(driver.getPageSource()).contains(text);
System.out.println(text+” is present in the web page”);
What are the different exceptions in Selenium web driver?
The different exceptions in Selenium web drivers are
WebDriverException
ElementNotVisibleException
NoAlertPresentException
NoSuchWindowException
NoSuchElementException
TimeoutException
StaleElementReferenceException
How to handle MouseHover?
By using moveToElement() method in Actions class
//Create an object to Actions class
Actions action = new Actions(driver);
action.moveToElement(element);
How to handle Frames?
driver.switchTo().frames(id/name/index/loactor);
How to switch from child frame to parent frame?
To switch back from child frame to parent frame use method parentFrame()
Syntax:
driver.switchTo().parentFrame();
Explain how to switch back from a frame?
To switch back from a frame use method defaultContent()
Syntax:
driver.switchTo().defaultContent();
What is the difference between “/” and “//”
Single Slash “/” – Single slash is used to create XPath with absolute path i.e. it locates the element from the root node.
Example:
/html/body/div[2]/div[4]/table/tbody/tr[4]/td/span
Double Slash “//” – Double slash is used to create XPath with relative path i.e. it locates the element from the current node.
Example:
//input[@name=’email’]
What is the difference between Assert and Verify in Selenium?
There is no difference between Assert and Verify in a positive scenario.
Assert: If the assert condition is true then the program control will execute the next test step but if the condition is false, the execution will stop and further test step will not be executed.
Verify: If the verify condition is true then the program control will execute the next test step but if the condition is false, the execution of the current step will be skipped and further test step will be executed.
What is the super interface of WebDriver?
SearchContext is the super interface of WebDriver
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”);
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”})
In what all case we have to go for “JavaScript executor”.
Consider FB main page after you login. When u scrolls down, the updates get loaded. To
handle this activity, there is no selenium command. So you can go for javascript to set
the scroll down value like driver.executeScript(“window.scrollBy(0,200)”, “”);
What are the test types supported by Selenium?
Selenium supports UI and functional testing. As well it can support performance testing
for reasonable load using selenium grid.
What are the different assertions in SIDE?
Assertions are like Assessors, but they verify that the state of the application conforms to what is expected. Examples include “make sure the page title is X” and “verify that this check box is checked”.
Assertions are like Accessors, but they verify that the state of the application conforms to what is expected. Examples include “make sure the page title is X” and “verify that this checkbox is checked”.
All Selenium Assertions can be used in 3 modes: “assert”, “verify”, and “waitFor”.
For example, you can “assertText”, “verifyText” and “waitForText”. When an “assert” fails, the test is aborted. When a “verify” fails, the test will continue execution, logging the failure. This allows a single “assert” to ensure that the application is on the correct page, followed by a bunch of “verify” assertions to test form field values, labels, etc.
“waitFor” commands wait for some condition to become true (which can be useful for testing Ajax applications). They will succeed immediately if the condition is already true. However, they will fail and halt the test if the condition does not become true within the current timeout setting (see the setTimeout action below).
How to store a value which is text box using web driver?
driver.findElement(By.id(“your Textbox”)).sendKeys(“your keyword”);
How to handle alerts and confirmation boxes.
Confirmation boxes and Alerts are handled in same way in selenium.
var alert = driver.switchTo().alert();
alert.dismiss(); //Click Cancel or Close window operation
alert.accept(); //Click OK
Handle Confirmation boxes via JavaScript,
driver.executeScript(“window.confirm = function(message){return true;};”);
How to mouse hover on an element?
Actions action = new Actions(webdriver);
WebElement we = webdriver.findElement(By.xpath(“html/body/div[13]/ul/li[4]/a”));
action.moveToElement(we).moveToElement(webdriver.findElement(By.xpath(“/expression-here”))).click().build().perform();
How to switch between the windows?
private void handlingMultipleWindows(String windowTitle) {
Set windows = driver.getWindowHandles();
for (String window : windows) {
driver.switchTo().window(window);
if (driver.getTitle().contains(windowTitle)) { return; } } }
How to switch between frames?
WebDriver’s driver.switchTo().frame() method takes one of the three possible arguments:
A number.
Select a frame by its (zero-based) index. That is, if a page has three frames, the first frame would be at index “0”, the second at index “1” and the third at index “2”. Once the frame has been selected, all subsequent calls on the WebDriver interface are made to that frame.
A name or ID.
Select a frame by its name or ID. Frames located by matching name attributes are always given precedence over those matched by ID.
A previously found WebElement.
Select a frame using its previously located WebElement. Get the frame by it’s id/name or locate it by driver.findElement() and you’ll be good.
Post a Comment