import java.util.Set;
package SeleniumPractise;
import java.util.Set;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
public class ActionsAndWindows {
@Test
public void newWindows() throws InterruptedException {
WebDriver driver = new FirefoxDriver();
driver.get("http://docs.seleniumhq.org/");
driver.manage().window().maximize();
System.out.println(driver.getTitle());
System.out.println(driver.getCurrentUrl());
Thread.sleep(3000);
WebElement element1 = driver.findElement(By.cssSelector("#menu_documentation a"));
Actions action1 = new Actions(driver);
action1.contextClick(element1).build().perform();
action1.sendKeys(Keys.ARROW_DOWN).build().perform();
action1.sendKeys(Keys.ARROW_DOWN).build().perform();
Thread.sleep(2000);
action1.sendKeys(Keys.ENTER).build().perform();
Thread.sleep(2000);
WebElement element2 = driver.findElement(By.cssSelector("#menu_projects a"));
Actions action2 = new Actions(driver);
action2.contextClick(element2).build().perform();
action2.sendKeys(Keys.ARROW_DOWN).build().perform();
action2.sendKeys(Keys.ARROW_DOWN).build().perform();
Thread.sleep(2000);
action2.sendKeys(Keys.ENTER).build().perform();
Thread.sleep(2000);
WebElement element3 = driver.findElement(By.cssSelector("#menu_support a"));
Actions action3 = new Actions(driver);
action3.contextClick(element3).build().perform();
action3.sendKeys(Keys.ARROW_DOWN).build().perform();
action3.sendKeys(Keys.ARROW_DOWN).build().perform();
Thread.sleep(2000);
action3.sendKeys(Keys.ENTER).build().perform();
Thread.sleep(2000);
Set<String> windows = driver.getWindowHandles();
System.out.println("Number of windows " + windows.size());
String neededWindow = "Selenium Projects";
for (String window : windows) {
WebDriver requiredWindow = driver.switchTo().window(window);
System.out.println(requiredWindow.getTitle());
if (requiredWindow.getTitle().equals(neededWindow)) {
System.out.println(requiredWindow.getTitle());
break;
}
}
driver.findElement(By.linkText("Selenium WebDriver")).click();
Thread.sleep(3000);
driver.close(); // Closes the current browser
Thread.sleep(3000);
driver.quit();
}
}
package SeleniumPractise;
import java.util.Set;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
public class ActionsAndWindows {
@Test
public void newWindows() throws InterruptedException {
WebDriver driver = new FirefoxDriver();
driver.get("http://docs.seleniumhq.org/");
driver.manage().window().maximize();
System.out.println(driver.getTitle());
System.out.println(driver.getCurrentUrl());
Thread.sleep(3000);
WebElement element1 = driver.findElement(By.cssSelector("#menu_documentation a"));
Actions action1 = new Actions(driver);
action1.contextClick(element1).build().perform();
action1.sendKeys(Keys.ARROW_DOWN).build().perform();
action1.sendKeys(Keys.ARROW_DOWN).build().perform();
Thread.sleep(2000);
action1.sendKeys(Keys.ENTER).build().perform();
Thread.sleep(2000);
WebElement element2 = driver.findElement(By.cssSelector("#menu_projects a"));
Actions action2 = new Actions(driver);
action2.contextClick(element2).build().perform();
action2.sendKeys(Keys.ARROW_DOWN).build().perform();
action2.sendKeys(Keys.ARROW_DOWN).build().perform();
Thread.sleep(2000);
action2.sendKeys(Keys.ENTER).build().perform();
Thread.sleep(2000);
WebElement element3 = driver.findElement(By.cssSelector("#menu_support a"));
Actions action3 = new Actions(driver);
action3.contextClick(element3).build().perform();
action3.sendKeys(Keys.ARROW_DOWN).build().perform();
action3.sendKeys(Keys.ARROW_DOWN).build().perform();
Thread.sleep(2000);
action3.sendKeys(Keys.ENTER).build().perform();
Thread.sleep(2000);
Set<String> windows = driver.getWindowHandles();
System.out.println("Number of windows " + windows.size());
String neededWindow = "Selenium Projects";
for (String window : windows) {
WebDriver requiredWindow = driver.switchTo().window(window);
System.out.println(requiredWindow.getTitle());
if (requiredWindow.getTitle().equals(neededWindow)) {
System.out.println(requiredWindow.getTitle());
break;
}
}
driver.findElement(By.linkText("Selenium WebDriver")).click();
Thread.sleep(3000);
driver.close(); // Closes the current browser
Thread.sleep(3000);
driver.quit();
}
}
Output:
Selenium - Web Browser Automation
http://docs.seleniumhq.org/
Number of windows 4
Selenium - Web Browser Automation
Selenium Documentation — Selenium Documentation
Selenium Projects
Selenium Projects
No comments:
Post a Comment