List of usage examples for org.openqa.selenium WebDriver close
void close();
From source file:com.mycompany.mavenproject1.main.java
public static void main(String[] args) throws InterruptedException { String exePath = "C:\\Unit Testing\\Drivers\\chromedriver.exe"; Map<String, Object> prefs = new HashMap<String, Object>(); prefs.put("profile.default_content_setting_values.notifications", 2); ChromeOptions options = new ChromeOptions(); options.setExperimentalOption("prefs", prefs); System.setProperty("webdriver.chrome.driver", exePath); WebDriver driver = new ChromeDriver(options); JavascriptExecutor jse = (JavascriptExecutor) driver; WebDriverWait wait = new WebDriverWait(driver, 10); driver.get("https://www.facebook.com"); String actualTitle = driver.getTitle(); if (actualTitle.contentEquals("Facebook - Log In or Sign Up")) { driver.findElement(By.id("email")).sendKeys(""); driver.findElement(By.id("pass")).sendKeys(""); TimeUnit.SECONDS.sleep(1); driver.findElement(By.id("u_0_o")).submit(); System.out.println("Test Passed!"); } else {//from ww w . ja v a 2s. co m System.out.println("test is failed"); driver.close(); return; } int x = 0; while (x < 2) { jse.executeScript("scroll(0, 100000);"); x++; TimeUnit.SECONDS.sleep(1); } try { jse.executeScript("scroll(0, 0);"); List<WebElement> click = driver.findElements(By.xpath("//*[@class='comment_link _5yxe']")); List<WebElement> Names = driver.findElements(By.xpath("//*[@class=' UFICommentActorName']")); System.out.println(Names.size() + "This is how many elements in Names"); List<WebElement> comments = driver.findElements(By.xpath("//*[@class='UFICommentBody']")); for (WebElement el : click) { if (!el.getText().isEmpty()) { //System.out.println(el.getText()); System.out.println("Attempting to click " + el.getText()); jse.executeScript( "return arguments[0].scrollIntoView(0, document.documentElement.scrollHeight-10);", el); TimeUnit.MILLISECONDS.sleep(500); el.click(); } else { System.out.println("This is what we got from the web element\n"); System.out.println(el.getText()); } } jse.executeScript("int x = 0;" + "scroll(0, 0);"); TimeUnit.SECONDS.sleep(1); for (WebElement el : Names) { jse.executeScript( "return arguments[0].scrollIntoView(0, document.documentElement.scrollHeight-10);", el); TimeUnit.SECONDS.sleep(1); System.out.println(el.getText()); } } catch (NoSuchElementException e) { System.err.println("too slow on the scrolling"); } }
From source file:com.mycompany.webcalculator.OperationsSeleniumTest.java
public void dispose(WebDriver driver) { driver.close(); driver.quit(); }
From source file:com.partnet.automation.selenium.AbstractConfigurableDriverProvider.java
License:Apache License
@Override public void end() { // suggestion to close web driver before quitting to prevent socket lock on // 7054/*w w w . ja va 2 s . c o m*/ // https://code.google.com/p/selenium/issues/detail?id=7272 // https://code.google.com/p/selenium/issues/detail?id=4790 WebDriver driver = this.get(); if (driver == null) { LOG.debug("No driver to stop"); return; } LOG.debug("Stopping driver"); //closing android browser is not supported if (!Browser.getBrowser(driver).isAndroid()) { driver.close(); } this.get().quit(); this.set(null); }
From source file:com.pentaho.ctools.cda.MondrianJNDI.java
License:Apache License
/** * ############################### Test Case 4 ############################### * * Test Case Name:/* ww w . j a v a2s .c o m*/ * Query URL * Description: * The test case pretends to validate return data when call query url. * Steps: * 1. Select the option 'Mdx...' * 2. Click in Cache this * 3. Set a period * 4. In the new window, check the schedule * 5. Remove the schedule */ @Test public void tc5_CacheThisSimple_ScheduleIsSetSuccessful() { this.log.info("tc5_CacheThisSimple_ScheduleIsSetSuccessful"); String selectedHours = "21"; /* * ## Step 1 */ Select select = new Select(this.elemHelper.FindElement(driver, By.id("dataAccessSelector"))); select.selectByVisibleText("Mdx Query on SampleData - Jndi"); //wait to render page this.elemHelper.WaitForElementInvisibility(driver, By.cssSelector("div.blockUI.blockOverlay")); //Check the presented contains WebElement elemStatus = this.elemHelper.FindElement(driver, By.id("status")); assertEquals("Shipped", elemStatus.getAttribute("value")); //Check we have three elements and no more than that String textPaging = this.elemHelper.WaitForElementPresentGetText(driver, By.id("contents_info")); assertEquals("View 1 to 3 of 3 elements", textPaging); /* * ## Step 2 */ //Click in 'Cache this' this.elemHelper.ClickJS(driver, By.id("cachethis")); this.elemHelper.WaitForElementPresenceAndVisible(driver, By.id("dialog")); String questionActual = this.elemHelper.WaitForElementPresentGetText(driver, By.cssSelector("p.dialogTitle")); String questionExpect = "What schedule should this query run on? (advanced)"; assertEquals(questionExpect, questionActual); /* * ## Step 3 */ String parentWindowHandle = driver.getWindowHandle(); Select selectPeriod = new Select(this.elemHelper.FindElement(driver, By.id("periodType"))); selectPeriod.selectByValue("1"); //every day this.elemHelper.FindElement(driver, By.xpath("//input[@id='startAt']")).clear(); this.elemHelper.FindElement(driver, By.xpath("//input[@id='startAt']")).sendKeys(selectedHours); this.elemHelper.FindElement(driver, By.linkText("Ok")).click(); Set<String> listWindows = driver.getWindowHandles(); //wait for popup render this.elemHelper.WaitForNewWindow(driver); listWindows = driver.getWindowHandles(); //Get popup id WebDriver cdaCacheManager = null; Iterator<String> iterWindows = listWindows.iterator(); while (iterWindows.hasNext()) { String windowHandle = iterWindows.next(); cdaCacheManager = driver.switchTo().window(windowHandle); if (cdaCacheManager.getTitle().equals("CDA Cache Manager")) { break; } } //Validate page: //Title String titleCdaCacheManager = cdaCacheManager.getTitle(); assertEquals("CDA Cache Manager", titleCdaCacheManager); //Scheduled queries String subTitleText = this.elemHelper.WaitForElementPresentGetText(cdaCacheManager, By.xpath("//div[@id='scheduledQueries']/div")); assertEquals("Scheduled Queries", subTitleText); /* * ## Step 4 */ //Validate Query SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String strToday = sdf.format(new Date()); String queryName = this.elemHelper.WaitForElementPresentGetText(cdaCacheManager, By.xpath("//div[@id='lines']/div/div[1]")); String queryParam1 = this.elemHelper.WaitForElementPresentGetText(cdaCacheManager, By.xpath("//div[@id='lines']/div/div[2]//dl//dt")); String queryParam2 = this.elemHelper.WaitForElementPresentGetText(cdaCacheManager, By.xpath("//div[@id='lines']/div/div[2]//dl//dd")); String queryLExec = this.elemHelper.WaitForElementPresentGetText(cdaCacheManager, By.xpath("//div[@id='lines']/div/div[3]")); String queryNExec = this.elemHelper.WaitForElementPresentGetText(cdaCacheManager, By.xpath("//div[@id='lines']/div/div[4]")); String queryCron = this.elemHelper.WaitForElementPresentGetText(cdaCacheManager, By.xpath("//div[@id='lines']/div/div[5]")); String queryTime = this.elemHelper.WaitForElementPresentGetText(cdaCacheManager, By.xpath("//div[@id='lines']/div/div[6]")); String queryStatus = this.elemHelper.WaitForElementPresentGetText(cdaCacheManager, By.xpath("//div[@id='lines']/div/div[7]")); //AssertPopup for execute and delete WebElement delete = this.elemHelper.FindElement(cdaCacheManager, By.xpath("//a/img[@title='Remove Query']")); WebElement execute = this.elemHelper.FindElement(cdaCacheManager, By.xpath("//a/img[@title='Execute Query']")); assertNotNull(execute); assertNotNull(delete); //Validate execution details try { assertEquals("/public/plugin-samples/cda/cdafiles/mondrian-jndi.cda (1)", queryName); assertEquals("status", queryParam1); assertEquals("Shipped", queryParam2); assertThat("Last Execution: " + queryLExec, queryLExec, CoreMatchers.containsString("1970-01-01")); assertThat("Last Execution: " + queryLExec, queryLExec, CoreMatchers.containsString("00:00:00")); assertThat("Next Execution: " + queryNExec, queryNExec, CoreMatchers.containsString(strToday)); assertThat("Next Execution: " + queryNExec, queryNExec, CoreMatchers.containsString(selectedHours + ":00:00")); assertEquals("0 0 21 * * ? *", queryCron); assertEquals("-1", queryTime); assertEquals("Success", queryStatus); } catch (AssertionError ae) { this.log.error(ae.getMessage()); //Remove schedule this.removeFirstSchedule(); //Need guarantee we close everything cdaCacheManager.close(); driver.switchTo().window(parentWindowHandle); //raise the exception fail(ae.getMessage()); } /* * ## Step 5 */ this.removeFirstSchedule(); cdaCacheManager.close(); driver.switchTo().window(parentWindowHandle); assertTrue(driver.getWindowHandles().size() == 1); }
From source file:com.pentaho.ctools.cdf.JFreeChartComponent.java
License:Apache License
/** * ############################### Test Case 4 ############################### * * Test Case Name://from ww w . j av a 2 s. c om * Preview Chart * Description: * The test case pretends to validate when user press on Zoom a new window * is displayed. * Steps: * 1. Zoom on Bar Chart * 2. Zoom on Pie Chart */ @Test public void tc4_PreviewChart_NewWindowDisplayed() { this.log.info("tc4_PreviewChart_NewWindowDisplayed"); String title = ""; String attriStyle = ""; // ## Step 1 //Check bar title this.elemHelper.Click(driver, By.xpath("//div[@class='caption-details']")); this.elemHelper.WaitForAttributeValue(driver, By.xpath("//div[@class='caption-bottom']"), "style", "margin: -42px", 3); title = this.elemHelper.WaitForElementPresentGetText(driver, By.id("sampleObjectcaptiontitle")); assertTrue(title.equals("Top 10 Customers")); this.elemHelper.WaitForAttributeValue(driver, By.xpath("//div[@class='caption-bottom']"), "style", "margin: 0px", 10); //Check the bar is visible this.elemHelper.Click(driver, By.xpath("//div[@class='caption-details']")); this.elemHelper.WaitForAttributeValue(driver, By.xpath("//div[@class='caption-bottom']"), "style", "margin: -42px", 3); attriStyle = this.elemHelper.GetAttribute(driver, By.xpath("//div[@class='caption-bottom']"), "style"); assertTrue(attriStyle.contains("margin: -42px")); this.elemHelper.WaitForAttributeValue(driver, By.xpath("//div[@class='caption-bottom']"), "style", "margin: 0px", 10); //Click in Zoom this.elemHelper.Click(driver, By.xpath("//div[@class='caption-details']")); this.elemHelper.WaitForAttributeValue(driver, By.xpath("//div[@class='caption-bottom']"), "style", "margin: -42px", 3); this.elemHelper.Click(driver, By.xpath("//div[@id='sampleObjectcaptionzoom']")); this.elemHelper.WaitForAttributeValue(driver, By.xpath("//div[@class='caption-bottom']"), "style", "margin: 0px", 10); // NOTE - we have to wait for loading disappear this.elemHelper.WaitForElementInvisibility(driver, By.cssSelector("div.blockUI.blockOverlay")); WebDriver popup = null; String parentWindowHandle = driver.getWindowHandle(); // save the current window handle. Set<String> setWindows = driver.getWindowHandles(); //wait for popup render this.elemHelper.WaitForNewWindow(driver); setWindows = driver.getWindowHandles(); //Get popup id Iterator<String> windowIterator = setWindows.iterator(); while (windowIterator.hasNext()) { String windowHandle = windowIterator.next(); popup = driver.switchTo().window(windowHandle); if (popup.getTitle().isEmpty()) { break; } } String attrSrcPopup = this.elemHelper.GetAttribute(popup, By.cssSelector("img"), "src"); assertEquals(HttpStatus.SC_OK, HttpUtils.GetResponseCode(attrSrcPopup)); popup.close(); driver = driver.switchTo().window(parentWindowHandle); assertTrue(driver.getWindowHandles().size() == 1); driver.switchTo().defaultContent(); // ## Step 2 //Change to pie chart this.elemHelper.Click(driver, By.xpath("//div[@class='caption-details']")); this.elemHelper.WaitForAttributeValue(driver, By.xpath("//div[@class='caption-bottom']"), "style", "margin: -42px", 3); this.elemHelper.Click(driver, By.xpath("//div[@id='sampleObjectcaptionchartType']")); this.elemHelper.WaitForAttributeValue(driver, By.xpath("//div[@class='caption-bottom']"), "style", "margin: 0px", 3); // NOTE - we have to wait for loading disappear this.elemHelper.WaitForElementInvisibility(driver, By.cssSelector("div.blockUI.blockOverlay")); //Check bar title this.elemHelper.Click(driver, By.xpath("//div[@class='caption-details']")); this.elemHelper.WaitForAttributeValue(driver, By.xpath("//div[@class='caption-bottom']"), "style", "margin: -42px", 3); title = this.elemHelper.WaitForElementPresentGetText(driver, By.id("sampleObjectcaptiontitle")); assertTrue(title.equals("Top 10 Customers")); this.elemHelper.WaitForAttributeValue(driver, By.xpath("//div[@class='caption-bottom']"), "style", "margin: 0px", 3); //Check bar is visible this.elemHelper.Click(driver, By.xpath("//div[@class='caption-details']")); this.elemHelper.WaitForAttributeValue(driver, By.xpath("//div[@class='caption-bottom']"), "style", "margin: -42px", 3); attriStyle = this.elemHelper.GetAttribute(driver, By.xpath("//div[@class='caption-bottom']"), "style"); assertTrue(attriStyle.contains("margin: -42px")); this.elemHelper.WaitForAttributeValue(driver, By.xpath("//div[@class='caption-bottom']"), "style", "margin: 0px", 3); //Zoom this.elemHelper.Click(driver, By.xpath("//div[@class='caption-details']")); this.elemHelper.WaitForAttributeValue(driver, By.xpath("//div[@class='caption-bottom']"), "style", "margin: -42px", 3); this.elemHelper.Click(driver, By.xpath("//div[@id='sampleObjectcaptionzoom']")); this.elemHelper.WaitForAttributeValue(driver, By.xpath("//div[@class='caption-bottom']"), "style", "margin: 0px", 3); // NOTE - we have to wait for loading disappear this.elemHelper.WaitForElementInvisibility(driver, By.cssSelector("div.blockUI.blockOverlay")); parentWindowHandle = driver.getWindowHandle(); // save the current window handle. setWindows = driver.getWindowHandles(); //wait for popup render this.elemHelper.WaitForNewWindow(driver); setWindows = driver.getWindowHandles(); //Get popup id windowIterator = setWindows.iterator(); while (windowIterator.hasNext()) { String windowHandle = windowIterator.next(); popup = driver.switchTo().window(windowHandle); if (popup.getTitle().isEmpty()) { break; } } attrSrcPopup = this.elemHelper.GetAttribute(popup, By.cssSelector("img"), "src"); assertEquals(HttpStatus.SC_OK, HttpUtils.GetResponseCode(attrSrcPopup)); popup.close(); driver.switchTo().window(parentWindowHandle); assertTrue(driver.getWindowHandles().size() == 1); }
From source file:com.pentaho.ctools.cdf.require.JFreeChartComponent.java
License:Apache License
/** * ############################### Test Case 4 ############################### * * Test Case Name:/*from www .j a va2 s . c om*/ * Preview Chart * Description: * The test case pretends to validate when user press on Zoom a new window * is displayed. * Steps: * 1. Zoom on Bar Chart * 2. Zoom on Pie Chart */ @Test public void tc4_PreviewChart_NewWindowDisplayed() { this.log.info("tc4_PreviewChart_NewWindowDisplayed"); String title = ""; String attriStyle = ""; /* * ## Step 1 */ //Check bar title this.elemHelper.Click(driver, By.xpath("//div[@class='caption-details']")); this.elemHelper.WaitForAttributeValue(driver, By.xpath("//div[@class='caption-bottom']"), "style", "margin: -42px", 3); title = this.elemHelper.WaitForElementPresentGetText(driver, By.id("sampleObjectcaptiontitle")); assertTrue(title.equals("Top 10 Customers")); this.elemHelper.WaitForAttributeValue(driver, By.xpath("//div[@class='caption-bottom']"), "style", "margin: 0px", 10); //Check the bar is visible this.elemHelper.Click(driver, By.xpath("//div[@class='caption-details']")); this.elemHelper.WaitForAttributeValue(driver, By.xpath("//div[@class='caption-bottom']"), "style", "margin: -42px", 3); attriStyle = this.elemHelper.GetAttribute(driver, By.xpath("//div[@class='caption-bottom']"), "style"); assertTrue(attriStyle.contains("margin: -42px")); this.elemHelper.WaitForAttributeValue(driver, By.xpath("//div[@class='caption-bottom']"), "style", "margin: 0px", 10); //Click in Zoom this.elemHelper.Click(driver, By.xpath("//div[@class='caption-details']")); this.elemHelper.WaitForAttributeValue(driver, By.xpath("//div[@class='caption-bottom']"), "style", "margin: -42px", 3); this.elemHelper.Click(driver, By.xpath("//div[@id='sampleObjectcaptionzoom']")); this.elemHelper.WaitForAttributeValue(driver, By.xpath("//div[@class='caption-bottom']"), "style", "margin: 0px", 10); // NOTE - we have to wait for loading disappear this.elemHelper.WaitForElementInvisibility(driver, By.cssSelector("div.blockUI.blockOverlay")); WebDriver popup = null; String parentWindowHandle = driver.getWindowHandle(); // save the current window handle. Set<String> setWindows = driver.getWindowHandles(); //wait for popup render this.elemHelper.WaitForNewWindow(driver); setWindows = driver.getWindowHandles(); //Get popup id Iterator<String> windowIterator = setWindows.iterator(); while (windowIterator.hasNext()) { String windowHandle = windowIterator.next(); popup = driver.switchTo().window(windowHandle); if (popup.getTitle().isEmpty()) { break; } } String attrSrcPopup = this.elemHelper.GetAttribute(popup, By.cssSelector("img"), "src"); assertEquals(HttpStatus.SC_OK, HttpUtils.GetResponseCode(attrSrcPopup)); popup.close(); driver = driver.switchTo().window(parentWindowHandle); assertTrue(driver.getWindowHandles().size() == 1); driver.switchTo().defaultContent(); /* * ## Step 2 */ //Change to pie chart this.elemHelper.Click(driver, By.xpath("//div[@class='caption-details']")); this.elemHelper.WaitForAttributeValue(driver, By.xpath("//div[@class='caption-bottom']"), "style", "margin: -42px", 3); this.elemHelper.Click(driver, By.xpath("//div[@id='sampleObjectcaptionchartType']")); this.elemHelper.WaitForAttributeValue(driver, By.xpath("//div[@class='caption-bottom']"), "style", "margin: 0px", 3); // NOTE - we have to wait for loading disappear this.elemHelper.WaitForElementInvisibility(driver, By.cssSelector("div.blockUI.blockOverlay")); //Check bar title this.elemHelper.Click(driver, By.xpath("//div[@class='caption-details']")); this.elemHelper.WaitForAttributeValue(driver, By.xpath("//div[@class='caption-bottom']"), "style", "margin: -42px", 3); title = this.elemHelper.WaitForElementPresentGetText(driver, By.id("sampleObjectcaptiontitle")); assertTrue(title.equals("Top 10 Customers")); this.elemHelper.WaitForAttributeValue(driver, By.xpath("//div[@class='caption-bottom']"), "style", "margin: 0px", 3); //Check bar is visible this.elemHelper.Click(driver, By.xpath("//div[@class='caption-details']")); this.elemHelper.WaitForAttributeValue(driver, By.xpath("//div[@class='caption-bottom']"), "style", "margin: -42px", 3); attriStyle = this.elemHelper.GetAttribute(driver, By.xpath("//div[@class='caption-bottom']"), "style"); assertTrue(attriStyle.contains("margin: -42px")); this.elemHelper.WaitForAttributeValue(driver, By.xpath("//div[@class='caption-bottom']"), "style", "margin: 0px", 3); //Zoom this.elemHelper.Click(driver, By.xpath("//div[@class='caption-details']")); this.elemHelper.WaitForAttributeValue(driver, By.xpath("//div[@class='caption-bottom']"), "style", "margin: -42px", 3); this.elemHelper.Click(driver, By.xpath("//div[@id='sampleObjectcaptionzoom']")); this.elemHelper.WaitForAttributeValue(driver, By.xpath("//div[@class='caption-bottom']"), "style", "margin: 0px", 3); // NOTE - we have to wait for loading disappear this.elemHelper.WaitForElementInvisibility(driver, By.cssSelector("div.blockUI.blockOverlay")); parentWindowHandle = driver.getWindowHandle(); // save the current window handle. setWindows = driver.getWindowHandles(); //wait for popup render this.elemHelper.WaitForNewWindow(driver); setWindows = driver.getWindowHandles(); //Get popup id windowIterator = setWindows.iterator(); while (windowIterator.hasNext()) { String windowHandle = windowIterator.next(); popup = driver.switchTo().window(windowHandle); if (popup.getTitle().isEmpty()) { break; } } attrSrcPopup = this.elemHelper.GetAttribute(popup, By.cssSelector("img"), "src"); assertEquals(HttpStatus.SC_OK, HttpUtils.GetResponseCode(attrSrcPopup)); popup.close(); driver.switchTo().window(parentWindowHandle); assertTrue(driver.getWindowHandles().size() == 1); }
From source file:com.pentaho.ctools.utils.ElementHelper.java
License:Apache License
/** * This method shall close current window and select previous window to carry on testing * /* w ww . jav a2 s.c o m*/ * @param driver * @param parentWindow */ public void SelectParentWindow(final WebDriver driver, final String parentWindow) { // Need guarantee we close everything WebDriver previewWindow = null; String currentWindowHandle = driver.getWindowHandle(); previewWindow = driver.switchTo().window(currentWindowHandle); previewWindow.close(); driver.switchTo().window(parentWindow); }
From source file:com.smash.revolance.ui.explorer.UserExplorer.java
License:Open Source License
private void exploreNewWindowChanges(Page prevPage, Element clickable, String oldWindowHandle) throws Exception { WebDriver oldBot = swithToNewWindow(oldWindowHandle); exploreChanges(prevPage, clickable); // Closing the new window after exploration has completed WebDriver oldOldBot = user.setBrowser(oldBot); oldOldBot.close(); getBrowser().switchTo().window(oldWindowHandle); }
From source file:com.thoughtworks.selenium.webdriven.commands.Close.java
License:Apache License
@Override protected Void handleSeleneseCommand(WebDriver driver, String ignored, String alsoIgnored) { driver.close(); return null; }
From source file:com.twiceagain.mywebdriver.startup.DemoBasicWebPageDumpArticles.java
License:Open Source License
/** * @param args the command line arguments *//*from w w w .ja v a2s .com*/ public static void main(String[] args) { WebDriver wd = Drivers.getDriver(Drivers.Config.defaultLocalFirefox()); WebPageBasic page = new WebPageBasic(wd); page.setXpDocuments(".//div[@class='esc-body']"); page.init("http://news.google.com"); page.processDocuments(new DocumentPrinter()); // Maximize page Height, then take screenshot // Will crash if in grid mode ... ??!! Drivers.adjustPageHeight(wd); page.addDebugOverlay(); Drivers.screenshot2File(wd, "googleNewsTest.png"); wd.close(); }