61) How to achieve database testing in Selenium?

//Make a connection to the database
Connection con = DriverManager.getConnection(dbUrl,username,password);

//Load the JDBC Driver using the code
Class.forName("com.mysql.jdbc.Driver");

//Send queries to the database
Statement stmt = con.createStatement();

//Once the statement object is created use the executeQuery method to execute the SQL queries
stmt.executeQuery(select * from employee;);

//Results from the executed query are stored in the ResultSet Object. While loop to iterate through all data
while(rs.next())
{
String myName = rs.getString(1);
}

//Close the db connection
con.close();


62) What is Continuous Integration?

Continuous Integration (CI) is a development practice that requires developers to integrate code into a shared repository several times a day.Each check-in is then verified by an automated build, allowing teams to detect problems early.

63) How to handle Chrome Browser notifications in Selenium?


// Create object of HashMap Class
Map<String, Object> prefs = new HashMap<String, Object>();

// Set the notification setting it will override the default setting
prefs.put("profile.default_content_setting_values.notifications", 2);

// Create object of ChromeOption class
ChromeOptions options = new ChromeOptions();

// Set the experimental option
options.setExperimentalOption("prefs", prefs);

// pass the options object in Chrome driver
WebDriver driver = new ChromeDriver(options);

64) How to scroll web page up and down using Selenium WebDriver?


To scroll using Selenium, you can use JavaScriptExecutor interface that helps to execute JavaScript methods through Selenium Webdriver.

Syntax:

JavascriptExecutor js = (JavascriptExecutor) driver;

js.executeScript("arguments[0].scrollIntoView();", Element);


65) What is the use of @Listener annotation in TestNG?

Listener is defined as interface that modifies the default TestNG’s behaviour. As the name suggests Listeners “listen” to the event defined in the selenium script and behave accordingly. It is used in selenium by implementing Listeners Interface. It allows customizing TestNG reports or logs. There are

Many types of TestNG listeners available:

  • IAnnotationTransformer
  • IAnnotationTransformer2
  • IConfigurable
  • IConfigurationListener
  • IExecutionListener
  • IHookable
  • IInvokedMethodListener
  • IInvokedMethodListener2
  • IMethodInterceptor
  • IReporter
  • ISuiteListener
  • ITestListener

66) How to perform right click (Context Click) action in Selenium WebDriver?

We can use Action class to provide various important methods to simulate user actions

//Instantiate Action Class
Actions actions = new Actions(driver);

//Retrieve WebElement to perform right click
WebElement btnElement = driver.findElement(By.id("rightClickBtn"));

//Right Click the button to display Context Menu
actions.contextClick(btnElement).perform();


67) What is TestNG Assert and list out some common assertions supported by TestNG?

Asserts helps us to verify the conditions of the test and decide whether test has failed or passed. A test is considered successful ONLY if it is completed without throwing any exception.

Some of the common assertions are:

  • assertEqual
  • assertTrue
  • assertFalse

68) How to perform drag and drop action in Selenium WebDriver?


//Actions class method to drag and drop
Actions builder = new Actions(driver);

WebElement from = driver.findElement(By.id("draggable"));

WebElement to = driver.findElement(By.id("droppable"));

//Perform drag and drop
builder.dragAndDrop(from, to).perform();


69) How to perform double click action in Selenium WebDriver?


Action class method doubleClick(WebElement) is required to be used to perform this user action.

//Instantiate Action Class
Actions actions = new Actions(driver);

//Retrieve WebElement to perform double click WebElement
btnElement = driver.findElement(By.id("doubleClickBtn"));

//Double Click the button
actions.doubleClick(btnElement).perform();


70) What is parameterized testing in TestNG?


To pass multiple data to the application at runtime, we need to parameterize our test scripts.
There are two ways by which we can achieve parameterization in TestNG:

• With the help of Parameters annotation and TestNG XML file.

@Parameters({“name”,”searchKey”})

• With the help of DataProvider annotation.

@DataProvider(name=“SearchProvider”)

71) How to highlight elements using Selenium WebDriver?


// Create the JavascriptExecutor object
JavascriptExecutor js=(JavascriptExecutor)driver;

// find element using id attribute
WebElement username= driver.findElement(By.id("email"));

// call the executeScript method
js.executeScript("arguments[0].setAttribute('style,'border: solid 2px red'');", username);

72) Have you used any cross browser testing tool to run Selenium Scripts on cloud?


Below tools can be used to run selenium scripts on cloud:
  • SauceLabs
  • CrossBrowserTesting

73) What are the DesiredCapabitlies in Selenium WebDriver and their use?


The Desired Capabilities Class helps us to tell the webdriver, which environment we are going to use in our test script.

The setCapability method of the DesiredCapabilities Class, can be used in Selenium Grid. It is used to perform a parallel execution on different machine configurations. It is used to set the browser properties (Ex. Chrome, IE),

Platform Name (Ex. Linux, Windows) that are used while executing the test cases.


74) How to create and run TestNG.xml?


Step 1: Add a new file to the project with name as testng.xml

Step 2: Add below given code in testng.xml

<suite name=“TestSuite”>
<test name=“Test1”>
<classes>
<class name=“TestClass” />
</classes>
</test>
</suite>

Step 3: Run the test by right click on the testng xml file and select Run As > TestNG Suite


75) How can we create a data driven framework using TestNG?

We can create data driven tests by using the DataProvider feature.

public class DataProviderTest 
{
private static WebDriver driver;
@DataProvider(name = "Authentication")
public static Object[][] credentials() {
return new Object[][] { { "testuser_1", "Test@123" }, { "testuser_2”, "Test@123" }};
}

// Here we are calling the Data Provider object with its Name

@Test(dataProvider = "Authentication")
public void test(String sUsername, String sPassword) 
{
driver = new FirefoxDriver(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.testqa.com");
driver.findElement(By.xpath(".//*[@id='account']/a")).click();
driver.findElement(By.id("log")).sendKeys(sUsername);
driver.findElement(By.id("pwd")).sendKeys(sPassword);
driver.findElement(By.id("login")).click();
driver.findElement(By.xpath(".//*[@id='account_logout']/a")).click();
driver.quit();
}
}

76) Mention the name of the Framework which you are using currently in your project, explain it in details along with its benefits?

Framework consists of the following tools:

Selenium, Eclipse IDE,Junit, Maven, Cucumber

File Formats Used in the Framework:

Properties file: We use properties file to store and retrieve different application and framework related configuration

Excel files: Excel files are used to pass multiple sets of data to the application.Following are the key components of the framework:

PageObject : It consists of all different page classes with their objects and methods

TestData: It stores the data files, Script reads test data from external data sources and executes test based on it

Features: It consists of functional test cases in the form of cucumber feature files written in gherkin format

StepDefinitions: It consists of different methods to implement each step of your feature files

TestRunner: It is the starting point for Junit to start executing your tests

Utilities: It consists of different reusable framework methods to perform different operations

Reports: It consists of different test reports in different formats along with screenshots:

Pom xml: It consists of all different project dependencies and plugins


77) How to run a group of test cases using TestNG?


Groups is one more annotation of TestNG which can be used in the execution of multiple tests.

public class Grouping{
@Test (groups = { “g1” })
public void test1() {
System.out.println(“This is group 1”);
}
@Test (groups = { “g2” })
public void test2() {
System.out.println(“This is group 2“);
}}

Create a testing xml file like this:

<suite name =“Suite”>
<test name = “Grouping”>
<groups>
<run>
<include name=“g1”>
</run>
</groups>
<classes>
<class name=“Grouping”>
</classes>
</test>
</suite>


78) What are the different ways for refreshing the page using Selenium WebDriver?

Browser refresh operation can be performed using the following ways in Selenium:

Refresh method
driver.manage().refresh();

Get method
driver.get(“https://www.google.com”);
driver.get(driver.getCurrentUrl());

Navigate method
driver.get(“https://www.google.com”);
driver.navigate.to(driver.getCurrentUrl());

SendKeys method
driver. findElement(By.id("username")).sendKeys(Keys.F5);


79) How to handle hidden elements in Selenium WebDriver?

We can use the JavaScriptExecutor to handle hidden elements.

JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("document.getElementById('displayed-text').value='text123'");


80) How can you find broken links in a page using Selenium WebDriver?

List<WebElement> elements = driver.findElements(By.tagName(“a”));
List finalList = new ArrayList();
for (WebElement element : elementList){
if(element.getAttribute("href") != null){
finalList.add(element);
}
}
return finalList;

Post a Comment

Previous Post Next Post