What is the difference between assert and verify commands?
Assert: Assert command checks whether the given condition is true or false. Let’s say we assert whether the given element is present on the web page or not. If the condition is true then the program control will execute the next test step but if the condition is false, the execution would stop and no further test would be executed.
Verify: Verify command also checks whether the given condition is true or false. Irrespective of the condition being true or false, the program execution doesn’t halt i.e. any failure during verification would not stop the execution and all the test steps would be executed.
XPath is used to locate a web element based on its XML path. XML stands for Extensible Markup Language and is used to store, organize and transport arbitrary data.
What is the difference between “/” and “//” in Xpath?
Single Slash “/” – Single slash is used to create Xpath with absolute path i.e. the xpath would be created to start selection from the document node/start node.
/html/body/div[3]/div[1]/form/table/tbody/tr[1]/td/input
Double Slash “//” – Double slash is used to create Xpath with relative path i.e. the xpath would be created to start selection from anywhere within the document.
//input[@id='email']
What do we mean by Selenium 1 and Selenium 2?
Selenium RC and WebDriver, in a combination, are popularly known as Selenium 2. Selenium RC alone is also referred as Selenium 1.
How do I launch the browser using WebDriver?
The following syntax can be used to launch Browser:
WebDriver driver = new FirefoxDriver();
WebDriver driver = new ChromeDriver();
WebDriver driver = new InternetExplorerDriver();
Here, WebDriver is an interface, driver is a reference variable, FirefoxDriver() is a Constructor, new is a keyword, and new FirefoxDriver() is an Object.
Is the FirefoxDriver a Class or an Interface?
FirefoxDriver is a Java class, and it implements the WebDriver interface.
What are the different types of Drivers available in WebDriver?
The different drivers available in WebDriver are:
- FirefoxDriver
- InternetExplorerDriver
- ChromeDriver
- SafariDriver
- OperaDriver
- AndroidDriver
- IPhoneDriver
- HtmlUnitDriver
What are the different types of waits available in WebDriver?
There are two types of waits available in WebDriver:
- Implicit Wait
- Explicit Wait
- Fluent Wait
Implicit Wait: Implicit waits are used to provide a default waiting time (say 30 seconds) between each consecutive test step/command across the entire test script. Thus, subsequent test step would only execute when the 30 seconds have elapsed after executing the previous test step/command.
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Explicit Wait: Explicit waits are used to halt the execution till the time a particular condition is met or the maximum time has elapsed. Unlike Implicit waits, explicit waits are applied for a particular instance only.
WebDriverWait wait = new WebDriverWait(driver,30);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath(“//div[contains(text(),’COMPOSE’)]”)));
Fluent Wait : Fluent Wait looks for a web element repeatedly at
regular intervals until timeout happens or until the object is found.
Fluent
Wait commands are most useful when interacting with web elements that
can take longer durations to load. This is something that often occurs
in Ajax applications.
//Declare and initialise a fluent wait
FluentWait wait = new FluentWait(driver);
//Specify the timout of the wait
wait.withTimeout(5000, TimeUnit.MILLISECONDS);
//Sepcify polling time
wait.pollingEvery(250, TimeUnit.MILLISECONDS);
//Specify what exceptions to ignore
wait.ignoring(NoSuchElementException.class)
How to type in a textbox using Selenium?
The user can use sendKeys(“String to be entered”) to enter the string in the textbox.
WebElement username = driver.findElement(By.id(“Email”));
username.sendKeys(“sth”);
How to clear the text in the text box using Selenium WebDriver?
By using clear() method
How can you find if an element in displayed on the screen?
- isDisplayed()
- isSelected()
- isEnabled()
boolean buttonPresence = driver.findElement(By.id(“gbqfba”)).isDisplayed();
boolean buttonSelected = driver.findElement(By.id(“gbqfba”)).isSelected();
boolean searchIconEnabled = driver.findElement(By.id(“gbqfb”)).isEnabled();
How can we get a text of a web element?
Get command is used to retrieve the inner text of the specified web element.
String Text = driver.findElement(By.id(“Text”)).getText();
How to get an attribute value using Selenium WebDriver?
By using getAttribute(value);
How to fetch the current page URL in Selenium?
To fetch the current page URL, we use getCurrentURL()
How to select value in a dropdown?
The value in the dropdown can be selected using WebDriver’s Select class.
Select selectByValue = new Select(driver.findElement(By.id(“SelectID_One”)));
selectByValue.selectByValue(“greenvalue”);
Select selectByVisibleText = new Select (driver.findElement(By.id(“SelectID_Two”)));
selectByVisibleText.selectByVisibleText(“Lime”);
Select selectByIndex = new Select(driver.findElement(By.id(“SelectID_Three”)));
selectByIndex.selectByIndex(2);
What are the different types of navigation commands?
Following are the navigation commands:
navigate().back() – The above command requires no parameters and takes back the user to the previous webpage in the web browser’s history.
driver.navigate().back();
navigate().forward() – This command lets the user to navigate to the next web page with reference to the browser’s history.
driver.navigate().forward();
navigate().refresh() – This command lets the user to refresh the current web page there by reloading all the web elements.
driver.navigate().refresh();
navigate().to() – This command lets the user to launch a new web browser window and navigate to the specified URL.
driver.navigate().to(“https://google.com”);
How to handle frame in WebDriver?
An inline frame acronym as iframe is used to insert another document within the current HTML document or simply a web page into a web page by enabling nesting.
driver.switchTo().frame(“ID of the frame“);
driver.switchTo().frame(driver.findElements(By.tagName(“iframe”).get(0));
driver.switchTo().frame(0);
driver.switchTo().frame(“name of the frame”);
driver.switchTo().defaultContent();
When do we use findElement() and findElements()?
findElement(): findElement() is used to find the first element in the current web page matching to the specified locator value. Take a note that only first matching element would be fetched.
Syntax: WebElement element = driver.findElement(By.xpath(“//div[@id=’example’]//ul//li”));
findElements(): findElements() is used to find all the elements in the current web page matching to the specified locator value. Take a note that all the matching elements would be fetched and stored in the list of WebElements.
List <WebElement> elementList = driver.findElements(By.xpath(“//div[@id=’example’]//ul//li”));
What is the difference between driver.close() and driver.quit command?
close():close() method closes the web browser window that the user is currently working on.
quit(): quit() method closes down all the windows that the program has opened.
How can we handle web-based pop up?
WebDriver offers the users with a very efficient way to handle these pop-ups using Alert interface.
- void dismiss() – The accept() method clicks on the “Cancel” button as soon as the pop-up window appears.
- void accept() – The accept() method clicks on the “Ok” button as soon as the pop-up window appears.
- String getText() – The getText() method returns the text displayed on the alert box.
- void sendKeys(String stringToSend) – The sendKeys() method enters the specified string pattern into the alert box.
Alert alert = driver.switchTo().alert();
alert.accept(); or alert.dismiss();
Can Selenium handle windows based pop up?
Selenium is an automation testing tool which supports only web application testing. Therefore, windows pop up cannot be handled using Selenium. But There are several third-party tools available for handling window based pop-ups along with the selenium like AutoIT, Robot class etc.
How to assert title of the web page?
//verify the title of the web page
assertTrue(“The title of the window is incorrect.”,driver.getTitle().equals(“Title of the page”));
How to mouse hover on a web element using WebDriver?
Used Action Interface to mouse hover on a drop down which then opens a list of options.
// Instantiating Action Interface
Actions actions=new Actions(driver);
// hovering on the dropdown
actions.moveToElement(driver.findElement(By.id("id of the dropdown"))).perform();
// Clicking on one of the items in the list options
WebElement subLinkOption=driver.findElement(By.id("id of the sub link"));
subLinkOption.click();
How to capture screenshot in WebDriver?
// Code to capture the screenshot
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
// Code to copy the screenshot in the desired location
FileUtils.copyFile(scrFile, new File("C:\\CaptureScreenshot\\google.jpg"))
What are Junit annotations?
Following are the JUnit Annotations:
- @Test: Annotation lets the system know that the method annotated as @Test is a test method. There can be multiple test methods in a single test script.
- @Before: Method annotated as @Before lets the system know that this method shall be executed every time before each of the test methods.
- @After: Method annotated as @After lets the system know that this method shall be executed every time after each of the test method.
- @BeforeClass: Method annotated as @BeforeClass lets the system know that this method shall be executed once before any of the test methods.
- @AfterClass: Method annotated as @AfterClass lets the system know that this method shall be executed once after any of the test methods.
- @Ignore: Method annotated as @Ignore lets the system know that this method shall not be executed.
What are the different types of frameworks?
- Data Driven Testing Framework: Data Driven Testing Framework helps the user segregate the test script logic and the test data from each other. It lets the user store the test data into an external database. The data is conventionally stored in “Key-Value” pairs. Thus, the key can be used to access and populate the data within the test scripts.
- Keyword Driven Testing Framework: The Keyword Driven testing framework is an extension to Data-driven Testing Framework in a sense that it not only segregates the test data from the scripts, it also keeps the certain set of code belonging to the test script into an external data file.
- Hybrid Testing Framework: Hybrid Testing Framework is a combination of more than one above mentioned frameworks. The best thing about such a setup is that it leverages the benefits of all kinds of associated frameworks.
- Behavior Driven Development Framework: Behavior Driven Development framework allows automation of functional validations in easily readable and understandable format to Business Analysts, Developers, Testers, etc.
What is Object Repository? How can we create Object Repository in Selenium?
Object Repository is used to store locators in a centralized location instead of hard-coding them within the scripts.We do create a property file (.properties) to store all the element locators and these property files act as an object repository in Selenium WebDriver.
What is Page Object Model in Selenium?
In the Page Object Model Design Pattern, each web page is represented as a class. All the objects related to a particular page of a web application are stored in a class.
What are the annotations available in TestNG?
@BeforeTest
@AfterTest
@BeforeClass
@AfterClass
@BeforeMethod
@AfterMethod
@BeforeSuite
@AfterSuite
@BeforeGroups
@AfterGroups
@Test