Pages

Friday, July 3, 2015

How To scroll the page

Make sure to import org.openqa.selenium.JavascriptExecutor;


\JavascriptExecutor js = (JavascriptExecutor)driver;
// if the element is on top. js.executeScript("scroll(250, 0)"); // if the element is on bottom. js.executeScript("scroll(0, 250)");


WebElement element = driver.findElement(By.id(""));         JavascriptExecutor js =(JavascriptExecutor)driver;         js.executeScript("window.scrollTo(0,"element.getLocation().y+")");         element.click();


 You can also try the below using X or Y position
        WebElement element = driver.findElement(By.id(""));
        JavascriptExecutor js =(JavascriptExecutor)driver;
        js.executeScript("window.scrollTo(0,"element.getLocation().y+")");
        element.click();

How to Install TestNG step by step with screen shot

How to Install TestNG step by step

We will follow the below steps to install TestNG in Eclipse IDE .
NOTE: If your using eclipse latest version, please use Eclipse Marketplace option which is very simple, explained at the bottom.

There are two ways in installing TestNG in Eclipse

First Way on installing Eclipse is using "Install new software" option.
Second way is using "Eclipse Market Place". - This option will be available in new versions of eclipse.

Steps to Install Eclipse using Install new Software:

Step 1:
In Eclipse, on top menu bar, Under Help Menu, Click on "Install new Software" in help window.
Install New Software
Step 2:
Enter the URL (http://beust.com/eclipse/) at Work With field and click on "Add" button.
TestNg URL
Step 3:
Once you click on "Add", it will display the screen, Enter the Name as "TestNG".
Add Respository
Step 4:
After clicking on "OK", it will scan and display the software available with the URL which you have mentioned.
Now select the checkbox at TestNG and Click on "Next" button.
Install TestNG
Step 5:
It will check for the requirement and dependencies before starting the installation.
If there is any problem with the requirements/dependencies, it will ask you to install them first before continuing with TestNG. Most of the cases it will successfully get installed nothing to worry about it.
TestNG Dependencies
Step 6: 
Once the above step is done, it will ask you to review the installation details. If your are ready or Ok to install TestNG, click on "Next" to continue.
TestNg review installation
Step 7: 
Accept the Terms of the license agreement and Click on "Finish" button.
TestNg Accept
Thats it... It will take few minutes to get installed.
Finally once the installation is done, you can if the TestNG is installed properly or Not.
Go to Windows Menu bar, and Mouse Over on "Show View" and Click on "Other" at the last as in the below screen shot.
TestNg check
Expand Java folder and see if the TestNg is available as in the below screen shot.
TestNg Java
UPDATE:
Now eclipse is coming with Marketplace plugin by default which is a rich client solution for installing solutions listed on Eclipse Marketplace directly from an Eclipse. We can easily find third-party plugins that users can add to their Eclipse installation by using search option.
Under help menu, we should have an option as 'Eclipse Marketplace..'. Click on it.
Eclipse Market Place TestNG
Enter text as 'TestNG' and click on GO to search. You will now see TestNG with install option (If TestNG is already installed, you will have 'Update' and 'Uninstall'. as in the below screen shot)
Eclipse Market Place Install TestNG

PopupsHandling In Selenium



import org.openqa.selenium.Alert; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.Test; public class PopupsHandling { WebDriver driver=new FirefoxDriver(); @Test public void ExampleForAlert() throws InterruptedException { driver.manage().window().maximize(); driver.get("file:///C:/path/alerts.html"); Thread.sleep(2000); driver.findElement(By.xpath("//button[@onclick='alertFunction()']")).click(); Alert alert=driver.switchTo().alert(); System.out.println(alert.getText()); alert.accept(); } }

How to handle javascript alerts, confirmation and prompts



// Get a handle to the open alert, prompt or confirmation
Alert alert = driver.switchTo().alert();
Alert is an interface. There below are the methods that are used
//Will Click on OK button.
alert.accept();
// Will click on Cancel button.
alert.dismiss()
//will get the text which is present on th Alert.
alert.getText();
//Will pass the text to the prompt popup
alert.sendkeys();

How to locate object through link text


Example: If an element is given like this:



1
WebElement element=driver.findElement(By.linkText("Name of the Link"));

How to locate object through name

Example: Let’s take the above example:
You can easily choose the element with the help of Name locator from the above example:
name = “login”
name = “password”


1
2
3
WebElement elementUsername = driver.findElement(By.name("login"));
 
WebElement elementPassword = driver.findElement(By.name("password"));

How to locate Object through ID

1
2
3
4
5
6
7
<form name="loginForm">Login
 
Username: <input id="username" type="text" name="login" />
 
Password: <input id="password" type="password" name="password" />
 
<input type="submit" name="signin" value="SignIn" /></form>





1
2
3
WebElement elementUsername = driver.findElement(By.id("username"));
 
WebElement elementPassword = driver.findElement(By.id("password"));