How to skip a test case in TestNG?
@Test(enabled=false) annotation is used to skip a test case in TestNG
How to find whether an element is a Multidrop down or not?
By using isMultiple()
For example:
Boolean b=driver.findElement(By.id(“year”)).isMultiple();
System.out.println(b);
It retuns true if the element is multi drop down else false
What is the difference between getText() and getAttribute() ?
getText(): It is used to retrieve the text of a Text Element from the web page
For Example:
String Text = driver.findElement(By.id(“Text”)).getText();
getAttribute(): It is used to retrieve the text from a Text Field that is already eneterd into the text field.
For Example:
String Text = driver.findElement(By.id(“username”)).getAttribute();
How to select value in a dropdown?
To handle drop down, an object to be created to the Select class.
Select dropDown= new Select(element);
The value in the dropdown can be selected using 3 ways:
Syntax:
selectByValue()
selectByVisibleText()
selectByIndex()
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);
@Test(enabled=false) annotation is used to skip a test case in TestNG
How to find whether an element is a Multidrop down or not?
By using isMultiple()
For example:
Boolean b=driver.findElement(By.id(“year”)).isMultiple();
System.out.println(b);
It retuns true if the element is multi drop down else false
What is the difference between getText() and getAttribute() ?
getText(): It is used to retrieve the text of a Text Element from the web page
For Example:
String Text = driver.findElement(By.id(“Text”)).getText();
getAttribute(): It is used to retrieve the text from a Text Field that is already eneterd into the text field.
For Example:
String Text = driver.findElement(By.id(“username”)).getAttribute();
Cognizant Selenium Recently Asked Interview Questions Answers |
How to select value in a dropdown?
To handle drop down, an object to be created to the Select class.
Select dropDown= new Select(element);
The value in the dropdown can be selected using 3 ways:
Syntax:
selectByValue()
selectByVisibleText()
selectByIndex()
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);
Post a Comment