protected File savePageSourceToFile(String fileName, WebDriver webdriver, boolean retryIfAlert) { File pageSource = new File(reportsFolder, fileName + ".html"); try { writeToFile(webdriver.getPageSource(), pageSource); } catch (UnhandledAlertException e) { if (retryIfAlert) { try { Alert alert = webdriver.switchTo().alert(); log.severe(e + ": " + alert.getText()); alert.accept(); savePageSourceToFile(fileName, webdriver, false); } catch (Exception unableToCloseAlert) { log.severe("Failed to close alert: " + unableToCloseAlert); } } else { printOnce("savePageSourceToFile", e); } } catch (UnreachableBrowserException e) { writeToFile(e.toString(), pageSource); return pageSource; } catch (Exception e) { writeToFile(e.toString(), pageSource); printOnce("savePageSourceToFile", e); } return pageSource; }
/** * Check if element exist? Log: 0:not log result into result file. 1:log result into result file. * * @param dr * @param object * @param log * @return [boolean] */ public boolean QIsElementPresent(WebDriver dr, By object, int log) { /* * log: 0:No Log Result ; 1:Log Result */ try { dr.findElement(object); if (log == 1) { logStep("", "TRUE", "Check item Exist: " + object.toString()); } return true; } catch (NotFoundException e) { if (log == 1) { logStep("", "TRUE", "Check item Exist: " + object.toString()); } return false; } }
public static void waitBetween(int time) { driver.manage().timeouts().implicitlyWait(time, TimeUnit.SECONDS); }
private void test_image_picker(WebDriver driver) { // Check both files exist File f1 = null; File f2 = null; try { ClassLoader cl = ImagePickerTest.class.getClassLoader(); f1 = new File(cl.getResource("image_picker_1.png").toURI()); assertTrue(f1.exists()); f2 = new File(cl.getResource("image_picker_2.png").toURI()); assertTrue(f2.exists()); } catch (URISyntaxException e) { e.printStackTrace(); } assertNotNull(f1); assertNotNull(f2); // Login CStudioSeleniumUtil.try_login( driver, CStudioSeleniumUtil.AUTHOR_USER, CStudioSeleniumUtil.AUTHOR_PASSWORD, true); // Navigate to Widget CStudioSeleniumUtil.navigate_to_image_picker_widget(driver); // Upload right file size CStudioSeleniumUtil.click_on(driver, By.id("xf-4$xf-20$imageSrcAltTextTest-imageButton")); // Check popup dialog appears WebElement popup = driver.findElement(By.id("cstudio-wcm-popup-div_c")); assertTrue(popup.isDisplayed()); WebElement input = driver.findElement(By.xpath("/html/body/div[3]/div/div[2]/div/div/div/form/input")); input.sendKeys(f1.getAbsolutePath()); WebElement upload = driver.findElement(By.xpath("/html/body/div[3]/div/div[2]/div/div/div/form/input[2]")); upload.click(); new WebDriverWait(driver, CStudioSeleniumUtil.SHORT_TIMEOUT) .until( new ExpectedCondition<Boolean>() { public Boolean apply(WebDriver d) { WebElement e = d.findElement(By.id("xf-4$xf-20$imageSrcAltTextTest$xf-206$$a")); return e != null && e.getAttribute("class").contains("xforms-alert-inactive"); } }); WebElement mark = driver.findElement(By.id("xf-4$xf-20$imageSrcAltTextTest$xf-206$$a")); assertTrue(mark.getAttribute("class").contains("xforms-alert-inactive")); // Delete uploaded image CStudioSeleniumUtil.click_on(driver, By.id("xf-4$xf-20$imageSrcAltTextTest-deleteButton")); WebElement filename = driver.findElement(By.id("xf-4$xf-20$imageSrcAltTextTest$xf-206-filename")); assertTrue(filename.getText().equals("250W X 130H")); // Upload any image CStudioSeleniumUtil.click_on(driver, By.id("xf-4$xf-20$imageSrcTest-imageButton")); // Check popup dialog appears popup = driver.findElement(By.id("cstudio-wcm-popup-div_c")); assertTrue(popup.isDisplayed()); input = driver.findElement(By.xpath("/html/body/div[3]/div/div[2]/div/div/div/form/input")); input.sendKeys(f2.getAbsolutePath()); upload = driver.findElement(By.xpath("/html/body/div[3]/div/div[2]/div/div/div/form/input[2]")); upload.click(); new WebDriverWait(driver, CStudioSeleniumUtil.SHORT_TIMEOUT) .until( new ExpectedCondition<Boolean>() { public Boolean apply(WebDriver d) { WebElement e = d.findElement(By.id("xf-4$xf-20$imageSrcTest$xf-237-filename")); return e != null && e.getText().equals("/static-assets/images/image_picker_2.png"); } }); // Delete uploaded image CStudioSeleniumUtil.click_on(driver, By.id("xf-4$xf-20$imageSrcTest-deleteButton")); filename = driver.findElement(By.id("xf-4$xf-20$imageSrcTest$xf-237-filename")); assertTrue(filename.getText().equals("")); // Close driver CStudioSeleniumUtil.exit(driver); }