Selenium Que
Selenium Que
Absolute path will start with root path (/) and Relative path will from current path (//)
2. Tell me some TestNG Annotations.
@Test,@Parameters,@Listeners,@BeforeSuite,@AfterSuite,@BeforeTest,@AfterTest,
@DataProvider,@BeforeGroups,@AfterGroups,@BeforeClass,@AfterClass,
@BeforeMethod,@AfterMethod,@Factory
3. What are desired capabilities?
Desired Capabilities help to set properties for the Web Driver. A typical use case would be
to
set the path for the Firefox Driver if your local installation doesn't correspond to the
default
settings.
4. Difference between Selenium RC and Selenium
Web driver.
Selenium RC
Selenium RCs architecture is way more
complicated.
Selenium RC is slower since it uses a JavaScript Web Driver is faster than Selenium RC
program called Selenium Core. This Selenium
since it speaks directly to the browser
Core is the one that directly controls the browser, uses the browsers own engine to control
not you.
it.
Selenium Core, just like other JavaScript codes, Web Driver interacts with page elements
can access disabled elements.
in a more realistic way.
Selenium RCs API is more matured but
contains redundancies and often confusing
commands.
such as application servers). You can use test suite,annotations, automatically generation
of report and much more.
10. What is the difference between thread.Sleep()
and selenium. Set Speed ("2000")?
If the application is taking time to load the page then we use selenium.waitforpageload("
"). This command is doesnt wait upto the given time whenever the page load is completed.
If the application is taking time to refresh the page, then we use Thread. Sleep ( ).it is a
standard wait it simply wait to the given time.
selenium.setSpeed
1. Takes a single argument in string format
Ex: selenium.setSpeed("2000") - will wait for 2 seconds
2. Runs each command in after setSpeed delay by the number of milliseconds mentioned in
set Speed.
thread.sleep
1. Takes a single argument in integer format
ex: thread. Sleep(2000) - will wait for 2 seconds
2. Waits for only once at the command given at sleep.
11. In what situation selenium finding element
get fails?
forward and do some functionality and back to the home page this can be achieved
through navigate() only. driver.get will wait till the whole page gets loaded and
driver.navigate
will just redirect to that page and will not wait
13. Please tell me the difference b/w implicitly
Wait and Explicit wait.
Implicit Wait sets internally a timeout that will be used for all consecutive Web Element
searches. It will try lookup the element again and again for the specified amount of time
before throwing a NoSuchElementException if the element could not have been found. It
does only this and can't be forced into anything else - it waits for elements to show up.
Explicit Wait or just Wait is a one-timer used by you for a particular search. It is more
extendible in the means that you can set it up to wait for any condition you might like.
Usually, you can use some of the prebuilt Expected Conditions to wait for elements to
become clickable, visible, invisible, etc., or just write your own condition that suits your
needs.
14. How we can retrieve the dynamically changing
Ids? When we login Facebook the login label's id
changes dynamically thus resulting in failure.
We have a hierarchy of locators and Facebook Is dynamic in nature,so we are not able to
use "id" for identification for after that we have remaining 7 locator's for that :2. xpath ()..
3. name..4. css.. 5. link text.. 6. partiallinktext...7.tag name. so u can use any one for
identifying it. Most probably u can use "xpath" or "css-locator" and if there r tag then
link text or partial-link text. it depend on u . But we never use id's in Ajax application
because its not possible.
15.What is the difference between driver.Close()
and driver.Quit () method?
Close() - It is used to close the browser or page currently which is having the focus.
Quit() - It is used to shut down the web driver instance or destroy the web driver instance
(Close all the windows)
16. How to scroll web element?--not browser
FirefoxProfile profile=new FirefoxProfile();
profile.setEnableNativeEvents(true);
WebDriver driver=new FirefoxDriver(profile);
driver.navigate("http://jqueryui.com/draggable/");
Thread.sleep(6000L);
WebElement element=driver.findElement(By.xpath("//div[@id='draggable']"));
Actions actn=new Actions(driver);
actn.dragAndDropBy(element, 50, 50).build().perform();
}
17. What is the basic use of Firefox profiles and
how can we use them using selenium?
A profile in Firefox is a collection of bookmarks, browser settings, extensions, passwords,
and history; in short, all of your personal settings.
We use them to change user agent, changing default download directory, changing versions
etc.
18. Customize the name of file going to be
downloaded?
You have to download AUTO IT.exe file and has to be install
and later you have create .au3 file (in this file you have to specify the commands in
VB script like your file name, where have to save, it will be easy may be 3 or 4 steps )
using AUTOIT...then right click the .au3 file you have to compile ....after that you will
get the .exe file with the name of .au3 file ..In eclipse you will give the code like this
<----ProcessBuildder ps = new ProcessBuilder("path of the .exe file of au3") .start();--->
19. How to handle internationalisation through
web driver?
FirefoxProfile profile = new FirefoxProfile();
profile.set Preference("intl.accept_languages","jp");
Web driver driver = new FirefoxDriver(profile); driver.get(google.com) will open google in
Japanese Lang
20. How to overcome same origin policy through
web driver?
Proxy server.
driver.findElement(By.xpath("//div[contains(@class,
'_586i')]")).sendKeys("abc");
driver.findelement(By.Xpath("Xpath ").getcssvalue("font-type);
driver.findelement(By.Xpath("Xpath ").getcssvalue("background-colour);
25. How to run tests in multiple browser parallel?
Is there any other option other than selenium grid?
You create a class with a method something like this:
driver.get(http://www.google.com");
}
}
26. How to prepare Customized html Report using
TestNG in hybrid framework.
Below are the 3 ways:
Junit: with the help of ANT.
TestNG: using inbuilt default.html to get the HTML report. Also XST reports from ANT,
Selenium, TestNG combination.
Using our own customized reports using XSL jar for converting XML content to HTML.
27. Whats the hierarchy of TestNG annotations?
Explain me about annotation hierarchy & execution
order?
1. org.testng.annotations.Parameters (implements
java.lang.annotation.Annotation)
2. org.testng.annotations.Listeners (implements
java.lang.annotation.Annotation)
3. org.testng.annotations.Test (implements
java.lang.annotation.Annotation)
4. org.testng.annotations.AfterMethod (implements
java.lang.annotation.Annotation)
5. org.testng.annotations.BeforeTest (implements
java.lang.annotation.Annotation)
6. org.testng.annotations.BeforeMethod (implements
java.lang.annotation.Annotation)
7. org.testng.annotations.Optional (implements
java.lang.annotation.Annotation)
8. org.testng.annotations.AfterTest (implements
java.lang.annotation.Annotation)
9. org.testng.annotations.Guice (implements
java.lang.annotation.Annotation)
10.org.testng.annotations.BeforeGroups (implements
java.lang.annotation.Annotation)
11.org.testng.annotations.ExpectedExceptions (implements
java.lang.annotation.Annotation)
12.org.testng.annotations.TestInstance (implements
java.lang.annotation.Annotation)
13.org.testng.annotations.NoInjection (implements
java.lang.annotation.Annotation)
14.org.testng.annotations.AfterSuite (implements
java.lang.annotation.Annotation)
15.org.testng.annotations.AfterClass (implements
java.lang.annotation.Annotation)
16.org.testng.annotations.AfterGroups (implements
java.lang.annotation.Annotation)
17.org.testng.annotations.DataProvider (implements
java.lang.annotation.Annotation)
18.org.testng.annotations.BeforeSuite (implements
java.lang.annotation.Annotation)
19.org.testng.annotations.BeforeClass (implements
java.lang.annotation.Annotation)
20.org.testng.annotations.Factory (implements
java.lang.annotation.Annotation)
21.org.testng.annotations.Configuration (implements
java.lang.annotation.Annotation)
22.org.testng.annotations.ObjectFactory (implements
java.lang.annotation.Annotation)
What is TestNG?
So far we had been doing Selenium tests without generating a proper format for the test
results. From this point on, we shall tackle how to make these reports using a test
framework called TestNG.
TestNG is a testing framework that overcomes the limitations of another popular
testing framework called JUnit. The "NG" means "Next Generation". Most Selenium
users use this more than JUnit because of its advantages. There are so many
features of TestNG, but we will only focus on the most important ones that we can
use in Selenium.Advantages of TestNG over JUnit
package programs;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class findbrokenimages {
static int invalidimg;
static WebDriver driver ;
public static void main(String[] args) {
try {
driver = new FirefoxDriver();
driver.get("http://ruchi-myseleniumblog.blogspot.in");
invalidimg = 0;
List allImages = driver.findElements(By.tagName("img"));
System.out.println("Total images are " + allImages.size());
for (int i = 0; i < allImages.size(); i++) {
WebElement img = (WebElement) allImages.get(i);
if (img != null) {
verifyimgActive(img);
}
}
System.out.println("Total invalid images are " + invalidimg);
driver.quit();
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
}
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class Parallelexecution {
private WebDriver driver = null;
@BeforeTest
@Parameters({ "BROWSER" })
public void setup(String BROWSER) {
System.out.println("Browser: " + BROWSER);
if (BROWSER.equals("FF")) {
System.out.println("Firefox Browser is selected");
driver = new FirefoxDriver();
} else if (BROWSER.equals("IE")) {
System.out.println("Internet Explorer Browser is selected");
driver = new InternetExplorerDriver();
} else if (BROWSER.equals("HU")) {
System.out.println("Html Unit Browser is selected");
driver = new HtmlUnitDriver();
} else if (BROWSER.equals("CH")) {
System.out.println("Google chrome Browser is selected");
Well, you could see the above Acronyms buzzing over all Automation folks. I was not sure
on what it means and How it differs each other. How each methodology will benefit? and
where exactly it will help in the Development Life cycle.
Finally, after some analysis I had found out the differences and posting it here. Readers are
always welcomed to correct me if I am wrong.
First lets list out what exactly each methodology does means
TDD Test Driven Development
Its also called test-driven design, is a method of software development in which unit testing
is repeatedly done on source code. Write your tests watch it fails and then refactor it. The
concept is we write these tests to check if the code we wrote works fine. After each test,
refactoring is done and then the same or a similar test is performed again. The process is
iterated as many times as necessary until each unit is functionally working as expected.
TDD was introduced first by XP. I believe I have explained enough in simple terms.
BDD Behaviour Driven Development
Behavior-driven development combines the general techniques and principles of TDD with
ideas from domain-driven design
DDD-Domain Driven Testing
BDD is similar in many ways to TDD except that the word test is replaced with the word
Behaviour. Its purpose is to help the the folks devising the system (i.e., the developer)
identify appropriate tests to writethat is, tests that reflect the behavior desired by the
stakeholders. BDD is usually done in very English-like language helps the Domain experts
to understand the implementation rather than exposing the code level tests. Its defined in a
GWT format, GIVEN WHEN & THEN.
44. How to change user agent in Firefox by selenium
web driver.
FirefoxProfile profile = new FirefoxProfile();
Mobile Automation
Pass
<option value="3">pqr</option>
</select>
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");
55. How to work with dynamic web table?
You can get the total number of <tr> tags within a <td> tag by giving the xpath of the
<td> element by using this function List<WebElement> ele = driver.findElements(By.xpath("Xpath of the table"));
Now you can use a for each loop to loop through each of the <tr> tags in the above list
and then read each value by using getText() method.
56. 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.
57. 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
58. 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
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).
66. How to store a value which is text box using
web driver?
driver.findElement(By.id("your Textbox")).sendKeys("your keyword");
67. 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;};");
68. 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();
69. How to switch between the windows?
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
public class testDragandDrop {
public static void main(String[] args) throws InterruptedException {
WebDriver driver = new FirefoxDriver();
driver.get("http://jqueryui.com/resources/demos/droppable/default.html");
WebElement draggable = driver.findElement(By.xpath("//*[@id='draggable']"));
WebElement droppable = driver.findElement(By.xpath("//*[@id='droppable']"));
Actions action = new Actions(driver);
action.dragAndDrop(draggable, droppable).perform();
}
}
Sliding an Element:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
public class testSlider {
/**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
WebDriver driver = new FirefoxDriver();
driver.get("http://jqueryui.com/resources/demos/slider/default.html");
WebElement slider = driver.findElement(By.xpath("//*[@id='slider']/a"));
Actions action = new Actions(driver);
Thread.sleep(3000);
action.dragAndDropBy(slider, 90, 0).perform();
}
}
Re-sizing an Element:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
public class testResizable {
public static void main(String[] args) throws InterruptedException {
WebDriver driver = new FirefoxDriver();
driver.get("http://jqueryui.com/resources/demos/resizable/default.html");
WebElement resize = driver.findElement(By.xpath("//*[@id='resizable']/div[3]"));
}
}
75. What is the default time for selenium Ide and
webdriver?
Default timeout in selenium ide is 30 seconds.
76. Write down scenarios which we can't automate?
Barcode Reader, Captcha etc.
77. In TestNG I have some test's Test1-Test2Test3-Test4-Test5I want to run my execution
order is Test5-Test1-Test3-Test2-Test4.How
do you set the execution order can you explain
for that?
FileOutputStream(outputFolder)));
BufferedInputStream in = null;
byte[] data = new byte[1000];
String files[] = inputFolder.list();
for (int j=0; j<files.length; i++)
{
in = new BufferedInputStream(new FileInputStream
(inputFolder.getPath() + "/" + files[j]), 1000);
out.putNextEntry(new ZipEntry(files[j]));
int totalcount;
while((totalcount= in.read(data,0,1000)) != -1)
{
out.write(data, 0, totalcount);
}
out.closeEntry();
}
out.flush();
out.close();
}
catch(Exception e)
{
e.printStackTrace();
return "Fail - " + e.getMessage();
}
}
80. What is default port no?
4444
81. If Default port no is busy how to change port no?
We can use any port number which is valid.. First create an object to remote control
configuration.
Use 'setPort' method and provide valid port number(4545,5555,5655, etc).. There after
attach this
remote control configuration object to selenium server..i.e
RemoteControlConfiguration r= new RemoteControlConfiguration();
r.setPort(4567);
2. @Test public void method() - Annotation @Test identifies that this method is a test
method.environment,e.g. read input data, initialize the class)
3. @After public void method() - Test method must start with test@Before - this annotation
is used for executing a method before
87. Difference between assert and verify in selenium
web driver.
When an assert fails, the test will be aborted. Assert is best used
when the
check value has to pass for the test to be able to continue to run log
in.
WebDriverException
NoAlertPresentException
NoSuchWindowException
NoSuchElementException
TimeoutException
iOS for phones (devices & emulators) and for tablets (devices &
emulators)