List of usage examples for org.openqa.selenium WebDriver switchTo
TargetLocator switchTo();
From source file:page.Crawl.java
License:Open Source License
public static boolean run(Node father, String url, WebDriver driver) { System.out.println(" > crawling url: " + url); try {//from www. j a v a 2s. c om driver.get(url); try { Thread.sleep(Globals.SLEEP_WAITING_FOR_PAGE_LOAD); } catch (Exception ex) { System.out.println("InterruptedException in crawl.run()"); ex.printStackTrace(); System.exit(-1); } List<WebElement> list = null; int old_size = 0; int new_size = 0; int loop = 1; do { //SCROLL TILL THE END OF THE PAGE old_size = new_size; int THRESHOLD = loop * Globals.BASE_SCROLL_STEP; for (int i = 0; i < THRESHOLD; i++) { JavascriptExecutor jse = (JavascriptExecutor) driver; jse.executeScript("window.scrollTo(0,document.body.scrollHeight)", ""); try { Thread.sleep(Globals.SLEEP_BEFORE_SCROLL_MS); } catch (Exception ex) { System.out.println("InterruptedException in crawl.run()"); ex.printStackTrace(); System.exit(-1); } } list = driver .findElements(By.xpath("//div[starts-with(@class, '" + Globals.MAIN_CLASS_NAME + "')]")); new_size = list.size(); //System.out.println("old: "+old_size+", new: "+new_size+", loop: "+loop); loop++; } while (old_size != new_size); if (new_size == 0) { return false; } for (WebElement element : list) { String date = ""; String id = ""; List<WebElement> ids = element .findElements(By.xpath(".//a[contains(@class, '" + Globals.ID_CLASS_NAME + "')]")); for (WebElement id_element : ids) { String hovercard = id_element.getAttribute("data-hovercard"); id = hovercard.replace(Globals.HOVERCARD_ID, ""); //String href = id_element.getAttribute("href"); //System.out.println("user: "+hovercard); //System.out.println("page: "+href); } List<WebElement> dates = element .findElements(By.xpath(".//abbr[contains(@class, '" + Globals.DATE_CLASS + "')]")); for (WebElement id_date : dates) { date = id_date.getAttribute("title"); //System.out.println("date: "+date); } if (id.equalsIgnoreCase("")) { System.out.println("ERRORE: id nullo per url " + url); return false; } Node current = new Node(id, date, father); List<WebElement> shared = element .findElements(By.xpath(".//a[contains(@class, '" + Globals.SHARE_CLASS + "')]")); for (WebElement id_shared : shared) { String href = id_shared.getAttribute("href"); //System.out.println("shares: "+href); //String text = id_shared.getText(); //System.out.println(text //DFS //OPEN NEW TAB WebElement body = driver.findElement(By.tagName("body")); body.sendKeys(Keys.CONTROL + "t"); try { Thread.sleep(Globals.SLEEP_WAITING_FOR_PAGE_LOAD); } catch (Exception ex) { System.out.println("InterruptedException in crawl.run()"); ex.printStackTrace(); System.exit(-1); } //driver.switchTo().window(tabs.get(1)); if (!run(current, href, driver)) { body.sendKeys(Keys.CONTROL + "w"); return false; } ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles()); driver.switchTo().window(tabs.get(tabs.size() - 1)); } father.addChildren(current); } } catch (Exception e) { System.out.println("WARNING: unable to crawl url: " + url); e.printStackTrace(); } //CLOSE CURRENT TAB WebElement body = driver.findElement(By.tagName("body")); body.sendKeys(Keys.CONTROL + "w"); return true; }
From source file:pawl.jbehave.step.BrowserSteps.java
License:Apache License
/** * Switch to a new window./* w w w. j a va 2s.c o m*/ */ @When("I switch to new window") public void switchToNewWindow() { final WebDriver driver = browser.base(); final String baseWindowHandle = driver.getWindowHandle(); final Set<String> opened = driver.getWindowHandles(); String newWindow; if (opened.size() > 1 && opened.remove(baseWindowHandle)) { final Iterator<String> iterator = opened.iterator(); newWindow = iterator.next(); } else { final WebDriverWait wait = new WebDriverWait(driver, Resources.base().explicitWait()); newWindow = wait.until(WebExpectedConditions.get().anyWindowOtherThan(opened)); } driver.switchTo().window(newWindow); }
From source file:qsp.popups.ActiTimewinhand.java
public static void main(String[] args) throws Throwable { WebDriver driver = new FirefoxDriver(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get("http://localhost/login.do"); driver.findElement(By.id("licenseLink")).click(); driver.findElement(By.xpath("//a[.='actiTIME Inc.']")).click(); Set<String> allwh = driver.getWindowHandles(); System.out.println(allwh.size()); for (String aw : allwh) { driver.switchTo().window(aw); System.out.println(driver.switchTo().window(aw)); driver.close();//from w w w . j a va 2 s . c om } }
From source file:ru.stqa.selenium.wait.RepeatableActions.java
License:Apache License
public static RepeatableAction<WebDriver, Alert> performSwitchToAlert() { return new AbstractRepeatableAction<WebDriver, Alert>() { @Override/*ww w . ja v a 2s . co m*/ public Alert apply(WebDriver driver) { return driver.switchTo().alert(); } @Override public boolean shouldIgnoreException(Throwable t) { return t instanceof NoAlertPresentException; } }; }
From source file:ru.stqa.selenium.wait.RepeatableActionsTest.java
License:Apache License
@Test public void switchToAlertActionShouldCallSwitchToAlert() { final WebDriver mockedDriver = mock(WebDriver.class); final WebDriver.TargetLocator mockedSwitch = mock(WebDriver.TargetLocator.class); final Alert mockedAlert = mock(Alert.class); when(mockedDriver.switchTo()).thenReturn(mockedSwitch); when(mockedSwitch.alert()).thenReturn(mockedAlert); Alert alert = new ActionRepeater<WebDriver>(mockedDriver, 1, 1).tryTo(performSwitchToAlert()); verify(mockedDriver, times(1)).switchTo(); verify(mockedSwitch, times(1)).alert(); assertThat(alert, is(mockedAlert));/*from w w w.j a va 2 s . c o m*/ }
From source file:ru.stqa.selenium.wait.RepeatableActionsTest.java
License:Apache License
@Test public void switchToAlertActionShouldIgnoreNoAlertPresentException() { final WebDriver mockedDriver = mock(WebDriver.class); final WebDriver.TargetLocator mockedSwitch = mock(WebDriver.TargetLocator.class); final Alert mockedAlert = mock(Alert.class); when(mockedDriver.switchTo()).thenReturn(mockedSwitch); when(mockedSwitch.alert()).thenThrow(NoAlertPresentException.class).thenThrow(NoAlertPresentException.class) .thenReturn(mockedAlert);/*w w w . j a v a 2 s . co m*/ Alert alert = new ActionRepeater<WebDriver>(mockedDriver, 1, 1).tryTo(performSwitchToAlert()); verify(mockedDriver, times(3)).switchTo(); verify(mockedSwitch, times(3)).alert(); assertThat(alert, is(mockedAlert)); }
From source file:ru.stqa.selenium.wrapper.EventFiringWrapperTest.java
License:Apache License
@Test public void canFireEventForAlertAccept() { final WebDriver mockedDriver = mock(WebDriver.class); final WebDriver.TargetLocator mockedTarget = mock(WebDriver.TargetLocator.class); final Alert mockedAlert = mock(Alert.class); final WebDriverListener mockedListener = mock(WebDriverListener.class); when(mockedDriver.switchTo()).thenReturn(mockedTarget); when(mockedTarget.alert()).thenReturn(mockedAlert); EventFiringWrapper wrapper = new EventFiringWrapper(mockedDriver); wrapper.addListener(mockedListener); final WebDriver driver = wrapper.getDriver(); driver.switchTo().alert().accept();/*from ww w . ja v a2 s . c om*/ verify(mockedAlert, times(1)).accept(); verify(mockedListener, times(1)).beforeAccept(mockedAlert); verify(mockedListener, times(1)).afterAccept(mockedAlert); }
From source file:sanity_ff.backup.Functions.java
public static void LFTLessthan25mbNeverExpire(Selenium selenium, WebDriver driver, String sender, String recipient, String subject, String emailBody, String senderPwd, String baseUrl) throws Exception { selenium.open(baseUrl);/*from www . ja va2s . c o m*/ selenium.type("id=id_username", sender); selenium.type("id=id_password", senderPwd); selenium.click("css=input[type=\"submit\"]"); selenium.waitForPageToLoad("2000"); System.out.println("First: The page title is " + selenium.getTitle()); // code to upload file driver.get(compose_url); selenium.waitForPageToLoad("3000"); Runtime.getRuntime().exec(pathToLessthan25MbFilesScript); Functions.MyWaitfunc(driver, "//*[@id='uploader_browse']"); WebElement ele = driver.findElement(By.xpath("//*[@id='uploader_browse']")); Thread.sleep(2000); ele.click(); Thread.sleep(3000); driver.findElement(By.id("addrin")).sendKeys(recipient); driver.findElement(By.id("id_subject")).sendKeys(subject); setExpirationToNever(driver); driver.findElement(By.id("addrsubmit")).click(); driver.switchTo().frame("id_body_ifr"); selenium.typeKeys("//body[@id='tinymce']", "Finally wohoooo!!"); driver.switchTo().defaultContent(); driver.findElement(By.id("submitter")).click(); // to check if mail was sent successfully String success_str_xpath = "//html/body/div/div[2]/div[4]/ul/li"; Functions.MyWaitfunc(driver, success_str_xpath); if ((Functions.doesWebElementExist(driver, By.xpath(success_str_xpath))) && (driver.findElement(By.xpath(success_str_xpath)).getText() .contains("Email sent to your outbox and enqueued for delivery."))) System.out.println("SUCCESS:Mail successfully sent !"); else { System.out.println("FAIL:Mail NOT SENT !"); System.out.println(driver.findElement(By.xpath(success_str_xpath)).getText()); Exception e1 = new Exception("This case FAILS"); throw e1; } driver.findElement(By.id("logout")).click(); }
From source file:sanity_ff.backup.Functions.java
public static void LFTLessthan25mbCustomExpire(String date, Selenium selenium, WebDriver driver, String sender, String recipient, String subject, String emailBody, String senderPwd, String baseUrl) throws Exception { selenium.open(baseUrl);/*from w ww .jav a2 s .c o m*/ selenium.type("id=id_username", sender); selenium.type("id=id_password", senderPwd); selenium.click("css=input[type=\"submit\"]"); selenium.waitForPageToLoad("2000"); System.out.println("First: The page title is " + selenium.getTitle()); // code to upload file driver.get(compose_url); selenium.waitForPageToLoad("3000"); Runtime.getRuntime().exec(pathToLessthan25MbFilesScript); Functions.MyWaitfunc(driver, "//*[@id='uploader_browse']"); WebElement ele = driver.findElement(By.xpath("//*[@id='uploader_browse']")); Thread.sleep(2000); ele.click(); Thread.sleep(3000); driver.findElement(By.id("addrin")).sendKeys(recipient); driver.findElement(By.id("id_subject")).sendKeys(subject); setCustomExpiration(driver, date); driver.findElement(By.id("addrsubmit")).click(); driver.switchTo().frame("id_body_ifr"); selenium.typeKeys("//body[@id='tinymce']", "Finally wohoooo!!"); driver.switchTo().defaultContent(); driver.findElement(By.id("submitter")).click(); // to check if mail was sent successfully String success_str_xpath = "//html/body/div/div[2]/div[4]/ul/li"; Functions.MyWaitfunc(driver, success_str_xpath); if ((Functions.doesWebElementExist(driver, By.xpath(success_str_xpath))) && (driver.findElement(By.xpath(success_str_xpath)).getText() .contains("Email sent to your outbox and enqueued for delivery."))) System.out.println("SUCCESS:Mail successfully sent !"); else { System.out.println("FAIL:Mail NOT SENT !"); System.out.println(driver.findElement(By.xpath(success_str_xpath)).getText()); Exception e1 = new Exception("This case FAILS"); throw e1; } driver.findElement(By.id("logout")).click(); }
From source file:sanity_ff.backup.Functions.java
public static void LFTGreaterThan25mbNeverExpire(Selenium selenium, WebDriver driver, String sender, String recipient, String subject, String emailBody, String senderPwd, String baseUrl) throws Exception { selenium.open(baseUrl);// w ww . j a v a 2 s . c o m selenium.type("id=id_username", sender); selenium.type("id=id_password", senderPwd); selenium.click("css=input[type=\"submit\"]"); selenium.waitForPageToLoad("2000"); System.out.println("First: The page title is " + selenium.getTitle()); // code to upload file driver.get(compose_url); selenium.waitForPageToLoad("3000"); Runtime.getRuntime().exec(pathToGreaterthan25MbFilesScript); Functions.MyWaitfunc(driver, "//*[@id='uploader_browse']"); WebElement ele = driver.findElement(By.xpath("//*[@id='uploader_browse']")); Thread.sleep(2000); ele.click(); Thread.sleep(3000); driver.findElement(By.id("addrin")).sendKeys(recipient); driver.findElement(By.id("id_subject")).sendKeys(subject); setExpirationToNever(driver); driver.findElement(By.id("addrsubmit")).click(); driver.switchTo().frame("id_body_ifr"); selenium.typeKeys("//body[@id='tinymce']", "Finally wohoooo!!"); driver.switchTo().defaultContent(); driver.findElement(By.id("submitter")).click(); // to check if mail was sent successfully String success_str_xpath = "//html/body/div/div[2]/div[4]/ul/li"; Functions.MyWaitfunc(driver, success_str_xpath); if ((Functions.doesWebElementExist(driver, By.xpath(success_str_xpath))) && (driver.findElement(By.xpath(success_str_xpath)).getText() .contains("Email sent to your outbox and enqueued for delivery."))) System.out.println("SUCCESS:Mail successfully sent !"); else { System.out.println("FAIL:Mail NOT SENT !"); System.out.println(driver.findElement(By.xpath(success_str_xpath)).getText()); Exception e1 = new Exception("This case FAILS"); throw e1; } driver.findElement(By.id("logout")).click(); }