How to load the Mobile
mode for Chrome at Run Time?
Chrome allows users to emulate Chrome on a mobile device (e.g. a
“Google Nexus 7” tablet, or an “Apple iPhone 5”) from the desktop version of
Chrome allows developers to quickly test how a website will render in a mobile
device, without requiring a real device. ChromeDriver can also enable Mobile
Emulation, via the “mobileEmulation” capability, specified with a dictionary
value.
Specifying a Known Mobile Device
To enable Mobile Emulation with a specific device name, the
“mobileEmulation” dictionary must contain a “deviceName.” Use a valid device
name from the DevTools Emulation panel as the value for
“deviceName.”
//Creating a mapper object to set the device Name
Map<String, String> mobileEmulation = new HashMap<String, String>();
mobileEmulation.put("deviceName", "Google Nexus 5");
Map<String, String> mobileEmulation = new HashMap<String, String>();
mobileEmulation.put("deviceName", "Google Nexus 5");
//Creating a mapper object to set the Emulation type
Map<String, Object> chromeOptions = new HashMap<String, Object>();
chromeOptions.put("mobileEmulation", mobileEmulation);
Map<String, Object> chromeOptions = new HashMap<String, Object>();
chromeOptions.put("mobileEmulation", mobileEmulation);
//Creating DesiredCapabilities to set the mobile emulation to
the driver object
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
//Creating driver object with the above created Capability
WebDriver driver = new ChromeDriver(capabilities);
WebDriver driver = new ChromeDriver(capabilities);
Selenium WebDriver API has a many different drivers to test your
web application in different browsers.
- Firefox
Driver - For Mozilla Firefox browser
- Internet
Explorer Driver - For Internet Explorer browser
- Chrome
Driver - For Google Chrome browser
- htmlUnit
Driver - GUI-Less(Headless) browser for Java programs
- Opera
Driver - For Opera browser
|
Locator
|
Usage
|
|
xPath
|
By.xpath("<xpath
of the Element>")
|
|
cssSelector
|
By.cssSelector ("<css
Selector of the Element>")
|
|
className
|
By.className("<Class
Name of the Element>")
|
|
id
|
By.id("<id of
the Element>")
|
|
name
|
By.name ("<name of
the Element>")
|
|
linkText
|
By.linkText ("<Link
Text of the Element>")
|
|
partialLinkText
|
By.partialLinkText ("<Partial
Link Text of the Element>")
|
|
tagName
|
By.tagName ("<Tag
Name of the Element>")
|
Selenium WebDriver is an open source tool; it will be available
in the market at no cost
- Open
Source.
- It
has multi-browser support.
- Multi-platform
support.
- Multiple
ways to spy/identify the object/element (id, className, xpath, cssSelector
etc.,).
- Web
as well mobile application testing support.
There are two types of waits available in selenium WebDriver
- Implicit
Wait
- Explicit
Wait
ImplicitlyWait: Sometimes, Elements are taking time to be appearing on web
application page. Using implicit wait in Selenium WebDriver, we can poll the
DOM for certain amount of time when some element or elements are not available
immediately on webpage.
Example:
Driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
If you will write above syntax in your test, your WebDriver test
will wait 10 seconds for appearing element on page.
ExplicitWait: Using explicit wait code In selenium WebDriver software
automation testing tool, You can define to wait for a certain condition to
occur before proceeding further test code execution.
Example:
WebDriverWait wait = new WebDriverWait(driver, 20);
Wait.until(expectedconditions.elementtobeclickable(By.xpath("//input[@id='gbqfq']")));
You can use java.lang.Thread.sleep(long milliseconds) method to
pause the software test execution for specific time.
Example:
Thread.sleep(10);
Selenium RC: When browser loaded In Selenium RC, It ‘injects’ JavaScript
functions into the browser and then it will use JavaScript to drive the browser
for the application under test.
Selenium WebDriver: Selenium WebDriver works like real user interacting with the application
and its elements. It will use each browser's native support to make direct
calls with browser for your software application under test. There is not any
Intermediate thing in selenium WebDriver to interact with web browsers.
It depends. If you are using only selenium WebDriver API to run
your tests and you are running all your tests on same machine then you do not
need selenium server because In this case, WebDriver can directly interact with
browser using browser's native support.
You need selenium server with WebDriver when you have to perform
bellow given operations with selenium WebDriver.
- When
you are using remote or virtual machine to run WebDriver tests for
software web application and that machine have specific browser version
that is not on your current machine.
- When
you are using selenium-grid to distribute your WebDriver's test execution
on different remote or virtual machines.
Driver.get("www.google.com");
No. It will not work and show you an exception like:
"Exception in thread "main"
org.openqa.selenium.WebDriverException: f.queryinterface is not a
function" when you run your test.
You need to provide http:// protocol with URL In driver.get
method as bellow.
Driver.get("http://www.google.com");
"Exception in thread "main"
org.openqa.selenium.NoSuchElementException: Unable to locate element"
We will get this exception when WebDriver is not able to locate
element on the page of software web application using whatever locator you have
used in your test. To resolve this Issue, I will check bellow given things.
- First
of all I will check that I have placed implicitlyWait code in my test or
not. If you have not placed implicitlyWait in your test and any element is
taking some time to appear on page then you can get this exception. So I
will add bellow given line at beginning of my test case code to wait for
15 seconds for element to be present on page. In 70% cases, this step will
resolve Issue.
Driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
- Another
reason behind this Issue is element's ID Is generated dynamically every
time when reloading the page. If I have used element's ID as an element
locator or used it in xpath to locate the element then I need to verify
that ID of element remains same every time or It Is changing? If it is
changing every time then I have to use alternative element locating
method. In 20% cases, this step will resolve your Issue.
- If
implicitlyWait is already added and element locator is fine then you need
to verify that how much time it (element) is taking to appear on page. If
It Is taking more than 15 seconds then you have to put explicitWait
condition with 20 or more seconds wait period as bellow. In 5 to 10%
cases, this step will resolve your Issue. View Example.
WebDriverWait wait = new WebDriverWait(driver, 25);
Wait.until(expectedconditions.elementtobeclickable(By.cssselector("#input")));
No. This is the biggest disadvantage of selenium WebDriver API.
We can automate only web and mobile software application's testing using
selenium WebDriver. If we want to automate desktop applications we have to use
third party tools like Sikuli or AutoIT
Can you tell me the alternative driver.get()
method to open URL in browser? What is the difference between them?
We can use anyone from below methods to open URL:
- driver.get()
- driver.navigate().to()
driver.get() method is generally used for Open URL of web application and it
will wait till the whole page gets loaded.
driver.navigate() method is generally used for navigate to URL of software
web application, navigate back, navigate forward, refresh the page. It will
just navigate to the page but it will not wait till the whole page
gets loaded.
No. WebDriver do not have any built in object repository. But we
can use .properties/.java file to store all the page objects / Locators as
follows.
In .properties file:
<variable Name> = <value>
btn_Login = //input[@class = ‘submit’]
In .java file:
<Access Modifier> final String
<Variable Name> = “<Locator Value>”;
public final String btn_Login =
“//input[@class = ‘submit’]”;
We can set browser window size using setSize method of selenium.
driver.manage().window().setSize(new Dimension(width, height));
Prompt dialog is just like confirmation alert dialog but with
option of Input text box
To Input value in that text box of prompt dialog, you can use
bellow given syntax.
driver.switchTo().alert().sendKeys("Input
text in the dialog box");
When I am running software web application's
tests in Firefox Browser using selenium webDriver, It is not showing me any
bookmarks, add-ons, saved passwords etc. in that browser. Do you know why?
Yes. It is because all those bookmarks, add-ons, passwords etc.,
are saved in your regular browser's profile folder so when you launch browser
manually, It will use existing profile settings so it will show you all those
stuffs. But when you run your software web application's tests in selenium
webDriver, it is opening new browser instance with blank/new profile. So It
will not show you bookmarks and all those things in that browser instance.
You can create custom Firefox profile and then you can use it in
selenium webDriver test. in your custom profile, you can set all required
bookmarks, add-ons etc..
Suppose we have 3 browsers namely
FirefoxDriver. HtmlUnitDriver and InternetExpolorerDriver. Arrange them in
fastest to slowest sequence?
HtmlUnitDriver is faster than all other drivers because
it is not using any UI to execute test cases of software web
application and InternetExplorerDriver is slower than Firefox and
HtmlUnitDriver.
So, Fastest to slowest driver sequence is as bellow.
- HtmlUnitDriver
- FirefoxDriver
- InternetExplorerDriver
Asynchronous JavaScript And XML
is full form of AJAX which is used for creating dynamic web pages very fast for
software web applications. Using ajax, We can update page behind the scene by
exchanging small amounts of data with server asynchronously. That means, Using
ajax we can update page data Without reloading page.
Generally we are using ImplicitlyWait in selenium webDriver
software automation tests to wait for some element to be present on page. But
Ajax call cannot be handled using only Implicit wait
in your test because page not get reloaded when Ajax call sent
and received from server and we cannot assume how much time It will take to
receive Ajax call from server.
To handle Ajax call in selenium webDriver software automation
tests, We needs to use webDriver’s ExplicitWait which can wait for specific
amount of time with specific condition.
On Google search page, I want to search for
some words without clicking on Google Search button. Is It possible in
webDriver? How?
Yes we can do it using webDriver sendKeys method where we do not
need to use Google Search button. Syntax is as bellow.
driver.findElement(By.xpath("//input[@id='gbqfq']")).sendKeys("Search
Syntax",Keys.ENTER);
In above syntax, //input[@id='gbqfq'] is xPath of Google search
text field. First it will enter "Search Syntax" text in text box and
then it will press Enter key on same text box to search for words on Google.
Can we perform drag and drop operation in
Selenium webDriver? Tell me a syntax to drag X element and drop It on Y
element.
Yes, we can perform drag and drop operation using selenium
webDriver software testing tool's Advanced User interactions API. Syntax is
like below.
new Actions(driver).dragAndDrop(X, Y).build().perform();
Yes, I have faced many technical challenges during selenium
webDriver test cases development and few of them are listed below.
- Sometimes,
Some elements like text box, buttons etc. are taking more time(more than
given Implicit wait time) to appear on page or to get enabled on page. In
such situation, if I have used only Implicit wait then my test case can
run fine on first run but it may fail to find element on second run. So we
need provide special treatment for such elements so that webDriver script
waits for element to be present or get enabled on page of software web
application during test execution. We can use ExplicitWait to handle this
situation.
- Handling
dynamic changing ID to locate element is tricky. if element's ID is
changing every time when you reload the application and you have to use
that ID in XPath to locate element then you have to use functions like
starts-with(@id, ‘post-body-') or contains(@id, ‘post-body-') in xPath.
- Clicking
on sub menus which are getting rendered on mouse hover of main menu is
somewhat tricky. You need to use webDriver's Actions class to perform
mouse hover operation.
- If
you have to execute your test cases in multiple browsers then one test
case can run successfully in Firefox browser but same test case may fail
in IE browser due to the timing related issues (NoSuchElementException)
because test execution in Firefox browser is faster than IE browser. You
can resolve this issue by increasing Implicit wait time when you run your
test in IE browser.
- Above
issue can arise due to the unsupported XPath in IE browser. In this case,
you need to you other locating methods (ID, Name etc.,) to locate element.
- Handling
JQuery elements like moving pricing slider, date picker, drag and drop
etc., is tricky. You should have knowledge of webDriver's Advanced User
interactions API to perform all these actions.
- Working
with multiple Windows, Frames, and some tasks like Extracting data from
web table, Extracting data from dynamic web table, Extracting all Links
from page, Extracting all text box from page are also tricky and time
consuming during test case preparation.
- There
is not any direct command to upload or download files from web page using
selenium webDriver. For downloading files using selenium webDriver, you
need to create and set Firefox browser profile with webDriver test case.
- WebDriver
do not have any built in object repository facility. You can do it using
java .properties or .java files to create object repository.
- webDriver
do not have any built in framework or facility using which we can achieve
below given tasks directly :
- Capturing
screenshots
- Generating
test execution log
- Reading
data from files
- Generating
test result reports, Manage test case execution sequence.
- To
achieve all these tasks, we have to use external services with webDriver
like Log4J to generate log, Apache POI API to read data from excel files,
TestNG XSLT reports to generate test result reports. TestNG to manage test
case execution, .properties file to create object repository. All these
tasks are very time consuming.
Can you tell me syntax to close current
webDriver instance and to close all opened webDriver instances?
driver.close(); To close current WebDriver instance
driver.quit(); To close all of them then we can use webDriver’s quit()
method
Is it possible to execute JavaScript directly
during software test execution? If Yes then tell me how to generate alert by
executing JavaScript in webDriver script?
Yes, we can execute JavaScript during webDriver software test
execution. To generate alert, you can write bellow given code in your script.
JavascriptExecutor JavaScript = (JavascriptExecutor) driver;
javascript.executeScript("alert('JavaScript Executed.');");
Give me syntax to read JavaScript alert
message string, clicking on OK button and clicking on Cancel button.
String alrtmsg = driver.switchTo().alert().getText(); //To Read
the message from the alert dialog
driver.switchTo().alert().accept(); //To click on OK button of
alert dialog
driver.switchTo().alert().dismiss(); //To click on CANCEL
button of alert dialog
- Desktop
Applications we can't automate. Need to user 3rd party tools to achieve
this.
- Bitmap
comparison is not possible using selenium webDriver software testing tool.
- Automating
captcha is not possible. (Few peoples say we can automate captcha but I am
telling you if you can automate any captcha then it is not a captcha).
- We
cannot read bar code using selenium webDriver software testing tool.
OTHER INTERVIEW QUESTIONS
I’m posting list of interview questions. Please answer them if
you know.
1.
How do Check whether two string are Equal or not in Java?
2. Can you write the
internal logic of the following String functions in Java
§ String.equals()
§ String.equalsIgnoreCase()
§ String.split()
§ String.strCat()
3. What is a Singleton
Class?
4. What is a Collection?
What are the collection concepts you have used in your Selenium? Which
situation and Why?
5. We have the following
DOM, how can you enter the value in the PASSWORD/2nd text box field? And what
are all the different approaches?
§ “<idiv id=”login_page“>
§ <iinput id=”user_name123“/>
§ <iinput/>
§ </idiv>”
6. Is the following
statement true for the above DOM
§ List<WebElement>
inputTags = driver.findElements(By.tagName(“//idiv[@id=’login_page’/iinput”));
7. What are different
frameworks and which one will you prefer and why?
8. Small Program which will
represent your framework?
9. What are the difference
between Keyword driven and POM approach
10. What are APIs will you
use for reading from excel and why jxl and POI?
11. Handling Web Table?
12. How will connect to the
Database?
13. Write the SQL query to
modify the employee salary whose department name contains letter ‘a’.
§ Employee Table:
§ Eno
§ Ename
§ Salary
§ Dept ID
§ Department Table:
§ Dept No
§ Dept Name
14. Tell me the scenario
where you have used any one of Oops concept in your functionality not in your
framework?
15. What is the difference
between FindElement and FindElements
16. What is the difference
between the following statements
§ WebDriver driver=new
FirefoxDriver()
§ FirefoxDriver driver
= new FirefoxDriver()
17. Keyword Driven Framework
Structure?
18. How do you maintain the Object
Repository in Selenium? And what are the different ways? (ex: Situation where
the application UI got changed)
19. How will you read the
values from configuration.properties file
20. What is Grid and Why it
is used?
21. Why JUNIT and TestNG? how
if we not use JUNIT or TestNG?
22. ** We have executed
100 Test cases out of which 10 Test cases are failed. So how can you run the failed test cases in TestNG and
what are the different approaches to achieve this?
23. Why do you use Xpath
instead of cssSelector?
24. How many ways can you
select the value from the dropdown?
25. How do you handle
windows?
26. How do you handle Alerts?
27. How do you handle the
Desktop alerts?
28. How do you upload a file?
29. How will you handle
different browser (IE and Chrome especially)
30. What are the different
Locators you have used and why are you not using remaining?
31. Can we achieve parallel
execution without using Grid?
32. What is a constructor?
What are the different Constructors? what you have used?
0 comments:
Post a Comment