Selenium WebDriver Methods Examples
1. Download the wget utility.
public class Uploadfile {
private WebDriver driver;
private String baseUrl;
@Before
public void setUp() {
driver = new FirefoxDriver();
baseUrl = "https://app.box.com/files";
}
@Test
public void testUploadfile() {
driver.get(baseUrl);
driver.findElement(By.id("login")).sendKeys("<username>");
driver.findElement(By.id("password")).sendKeys("<password>");
driver.findElement(By.id("continue")).click();
driver.findElement(By.id("upload_button")).click();
driver.findElement(By.name("upload_element")).sendKeys("<Path of the file to be upload>");
driver.findElement(By.id("popup_upload_button")).click();
}
}
___________________________________________________________________________________
1. Download the wget utility.
WebDriver driver;
@Before
public void setUp() {
driver = new FirefoxDriver();
driver.get("http://users.ugent.be/~bpuype/wget/");
}
@Test
public void downloadfile() {
WebElement anchortagDownload = driver.findElement(By.xpath("/html/body/blockquote/p[1]/a[2]"));
String href = anchortagDownload.getAttribute("href");
String wgetCommand;
wgetCommand = "cmd /c C:\\DevTools\\wget.exe -P C:\\temp2" + " " + href;
System.out.println(wgetCommand);
try {
Process exec = Runtime.getRuntime().exec(wgetCommand);
BufferedReader reader = new BufferedReader(new InputStreamReader(exec.getErrorStream()));
String line = null;
while((line = reader.readLine()) != null)
{
System.out.println(line);
}
exec.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
_______________________________________________________________________________________________
3. Use Selenium code to upload the file on box.
public class ImageHit {
ChromeDriver driver;
@Before
public void setUp() {
System.setProperty("webdriver.chrome.driver", "D:\\IntellijLib\\chromedriver.exe");
driver = new ChromeDriver();
driver.get("http://phenocare.com/");
}
@After
public void tearDown()
{
// driver.quit();
}
@Test
public void shouldoUnitTesting()
{
WebElement coaching = driver.findElement(By.linkText("Coaching"));
coaching.click();
WebElement element = driver.findElement(By.className("img-rounded"));
element.click();
}
}
_______________________________________________________________________________________________
public class ImageHit {
private FirefoxDriver driver;
@Before
public void setUp()
{
driver = new FirefoxDriver();
driver.get("http://newtours.demoaut.com/");
}
@Test
public void shouldDoUniTesting()
{
WebElement home = driver.findElement(By.linkText("Home"));
WebElement trHome = driver.findElement(By.xpath("/html/body/div/table/tbody/tr/td[1]/table/tbody/tr/td/table/tbody/tr/td/table/tbody/tr[1]"));
Actions actions =new Actions(driver);
org.openqa.selenium.interactions.Action build = actions.moveToElement(home).build();
System.out.println("Before Mouse Over" + trHome.getCssValue("background-color"));
build.perform();
System.out.println("After Mouse Over" + trHome.getCssValue("background-color"));
}
}
_______________________________________________________________________________________________
public class demoMouseEvent {
1.Browser Back and Forward (NAVIGATION)
Steps to implement Browser back and forward through Selenium Web Driver
1. Create Driver for any Browser(Mozilla or Chrome)
2. Go to the URL
3. Navigate to some page in website.
4. Use Selenium code to Navigate Back to Main Page.
CODE: driver.navigate().back();
driver.navigate().forward();
Example
WebDriver driver =new FirefoxDriver();
driver.get("http://seleniumhq.org/");
driver.findElement(By.linkText("Download")).click();
try {
Thread.sleep(3000); //delay
}
catch (Exception e){
System.out.println(e);
}
driver.navigate().back();
Thread.sleep(3000); //delay
}
catch (Exception e){
System.out.println(e);
}
driver.navigate().forward();
_______________________________________________________________________________________________
_______________________________________________________________________________________________
2.Verify The Functionality Of Successful Uploading File Using wget Utility
Steps to verify upload the file on particular website through Selenium Web Driver
1. Download the wget utility.
2. Create Driver for any Browser(Mozilla or Chrome)
3. Go to the URL in the code below and open the account for uploading the file.
4. Use Selenium code to upload the file on box.
4. Use Selenium code to upload the file on box.
public class Uploadfile {
private WebDriver driver;
private String baseUrl;
@Before
public void setUp() {
driver = new FirefoxDriver();
baseUrl = "https://app.box.com/files";
}
@Test
public void testUploadfile() {
driver.get(baseUrl);
driver.findElement(By.id("login")).sendKeys("<username>");
driver.findElement(By.id("password")).sendKeys("<password>");
driver.findElement(By.id("continue")).click();
driver.findElement(By.id("upload_button")).click();
driver.findElement(By.name("upload_element")).sendKeys("<Path of the file to be upload>");
driver.findElement(By.id("popup_upload_button")).click();
}
}
___________________________________________________________________________________
3.Verify The Functionality Of Successful Downloading file Using wget Utility
Steps to verify upload the file on particular website through Selenium Web Driver
1. Download the wget utility.
2. Create Driver for any Browser(Mozilla or Chrome)
3. Go to the URL in the code below and find xpath of the given link which is to be download.
4. Create folder before running the code where downloaded file is to be saved.
4. Use Selenium code to upload the file on box.
public class Uploadfile {4. Create folder before running the code where downloaded file is to be saved.
4. Use Selenium code to upload the file on box.
WebDriver driver;
@Before
public void setUp() {
driver = new FirefoxDriver();
driver.get("http://users.ugent.be/~bpuype/wget/");
}
@Test
public void downloadfile() {
WebElement anchortagDownload = driver.findElement(By.xpath("/html/body/blockquote/p[1]/a[2]"));
String href = anchortagDownload.getAttribute("href");
String wgetCommand;
wgetCommand = "cmd /c C:\\DevTools\\wget.exe -P C:\\temp2" + " " + href;
System.out.println(wgetCommand);
try {
Process exec = Runtime.getRuntime().exec(wgetCommand);
BufferedReader reader = new BufferedReader(new InputStreamReader(exec.getErrorStream()));
String line = null;
while((line = reader.readLine()) != null)
{
System.out.println(line);
}
exec.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
_______________________________________________________________________________________________
4.To Verify Presence of Image In Webpage By Using Selenium Webdriver
Steps to verify image is present on webpage.
1. Create Driver for any Browser(Mozilla or Chrome)
2. Go to the URL in the code below and locate the element by classname. 3. Use Selenium code to upload the file on box.
public class ImageHit {
ChromeDriver driver;
@Before
public void setUp() {
System.setProperty("webdriver.chrome.driver", "D:\\IntellijLib\\chromedriver.exe");
driver = new ChromeDriver();
driver.get("http://phenocare.com/");
}
@After
public void tearDown()
{
// driver.quit();
}
@Test
public void shouldoUnitTesting()
{
WebElement coaching = driver.findElement(By.linkText("Coaching"));
coaching.click();
WebElement element = driver.findElement(By.className("img-rounded"));
element.click();
}
}
_______________________________________________________________________________________________
5.To Check MouseOver Event With Particular Element on Webpage By Using Selenium Webdriver
public class ImageHit {
private FirefoxDriver driver;
@Before
public void setUp()
{
driver = new FirefoxDriver();
driver.get("http://newtours.demoaut.com/");
}
@Test
public void shouldDoUniTesting()
{
WebElement home = driver.findElement(By.linkText("Home"));
WebElement trHome = driver.findElement(By.xpath("/html/body/div/table/tbody/tr/td[1]/table/tbody/tr/td/table/tbody/tr/td/table/tbody/tr[1]"));
Actions actions =new Actions(driver);
org.openqa.selenium.interactions.Action build = actions.moveToElement(home).build();
System.out.println("Before Mouse Over" + trHome.getCssValue("background-color"));
build.perform();
System.out.println("After Mouse Over" + trHome.getCssValue("background-color"));
}
}
_______________________________________________________________________________________________
6.How To Perform Sequence Of Actions With Selenium Webdriver
We want to perform multiple actions in once like, drag-and-drop, sliding, selecting multiple items.
Below example shows where we can use the Actions Interface of Selenium Webdriver.
public class demoMouseEvent {
private FirefoxDriver driver;
@Before
public void setUp()
{
driver = new FirefoxDriver();
driver.get("http://newtours.demoaut.com/");
}
@Test
public void shouldDoUniTesting()
{
WebElement userName = driver.findElement(By.name("userName"));
Actions actions =new Actions(driver);
actions.moveToElement(userName)
.click()
.keyDown(userName, Keys.SHIFT)
.sendKeys("softedge")
.keyUp(userName, Keys.SHIFT)
.doubleClick(userName)
.build().perform();
}
}
___________________________________________________________________________________
We
want to perform multiple actions in once, like: drag-and-drop, sliding,
selecting multiple items. - See more at:
http://selenium.polteq.com/en/perform-a-sequence-of-actions-with-selenium-webdriver/#sthash.OwiNTDJq.dpuf
We
want to perform multiple actions in once, like: drag-and-drop, sliding,
selecting multiple items. - See more at:
http://selenium.polteq.com/en/perform-a-sequence-of-actions-with-selenium-webdriver/#sthash.OwiNTDJq.dpuf