Java tutorial
package server.system.scripts; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.util.List; import org.apache.log4j.Logger; import org.openqa.selenium.Alert; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.interactions.Actions; import org.testng.annotations.Test; import common.BaseTestScript; import common.CommonFunctions; import common.Driver; import common.XLDataReader; import jxl.Sheet; import navigation.MenuNavigation; public class ImportLicense extends BaseTestScript { private boolean blnStatus = false; private String temp = null; public boolean importLicence(WebDriver objWebDriver, String licFilePath, Logger accessLog) throws InterruptedException, IOException { boolean result = false; // Navigate to Licenses tab: try { CommonFunctions.navigateToTab(MenuNavigation.ImportLicense, objWebDriver, accessLog, null); // Check if the license is already imported or not, if not - import // the license: if (!objWebDriver.findElement(By.xpath(MenuNavigation.System.ImportLicense.spnLicStatusLW)) .getAttribute("title").toString().equalsIgnoreCase("License Active")) // Status // strings // for // validation // - // 'No // License' // 'License // Active' { // Click on Import License CommonFunctions.doClickAndSwitchToIFrame(objWebDriver, accessLog, By.xpath(MenuNavigation.System.ImportLicense.btnImportLicense), By.id(MenuNavigation.System.ImportLicense.frmIDImportLicense)); Thread.sleep(2000); // find and Click on Browse button WebElement btnBrowse = objWebDriver .findElement(By.xpath(MenuNavigation.System.ImportLicense.btnBrowse)); Actions builder = new Actions(objWebDriver); builder.moveToElement(btnBrowse).perform(); builder.doubleClick().perform(); // Call AutoIT exe String[] commands = { Driver.mConfigFile.getLibFolder() + File.separator + "import_license.exe", "Choose File to Upload", licFilePath }; try { Process proc; proc = Runtime.getRuntime().exec(commands); // Wait for AutoIT script to end successfully InputStream is = proc.getInputStream(); int retCode = 0; while (retCode != -1) { retCode = is.read(); blnStatus = true; } is.close(); } catch (IOException e1) { // TODO Auto-generated catch block accessLog.error("Caught exception while executing the AutoIT script"); e1.printStackTrace(); result = false; } // Click on Import button CommonFunctions.doClickAndWait(objWebDriver, By.xpath(MenuNavigation.System.ImportLicense.btnImport), By.xpath(MenuNavigation.System.ImportLicense.btnImportLicense), accessLog); Thread.sleep(1000); // Take screen shot if (blnStatus) { // validate that license has been imported successfully if (objWebDriver.findElement(By.xpath(MenuNavigation.System.ImportLicense.spnLicStatusWW)) .getAttribute("title").toString().equalsIgnoreCase("License Active") && objWebDriver .findElement(By.xpath(MenuNavigation.System.ImportLicense.spnLicStatusWS)) .getAttribute("title").toString().equalsIgnoreCase("License Active") && objWebDriver .findElement(By.xpath(MenuNavigation.System.ImportLicense.spnLicStatusLW)) .getAttribute("title").toString().equalsIgnoreCase("License Active") && objWebDriver .findElement(By.xpath(MenuNavigation.System.ImportLicense.spnLicStatusLS)) .getAttribute("title").toString().equalsIgnoreCase("License Active") && objWebDriver .findElement(By.xpath(MenuNavigation.System.ImportLicense.spnLicStatusMW)) .getAttribute("title").toString().equalsIgnoreCase("License Active")) { accessLog.info( "The license file : has been imported successfully and below are the details about active license"); accessLog .info("Windows Server : Status - " + objWebDriver .findElement(By .xpath(MenuNavigation.System.ImportLicense.spnLicStatusWS)) .getAttribute("title").toString() + ", Agents Allowed - " + objWebDriver .findElement(By.xpath( MenuNavigation.System.ImportLicense.spnAgntAllowedWS)) .getText().toString()); accessLog .info("Windows WorkStation : Status - " + objWebDriver .findElement(By .xpath(MenuNavigation.System.ImportLicense.spnLicStatusWW)) .getAttribute("title").toString() + ", Agents Allowed - " + objWebDriver .findElement(By.xpath( MenuNavigation.System.ImportLicense.spnAgntAllowedWW)) .getText().toString()); accessLog .info("Linux Server : Status - " + objWebDriver .findElement(By .xpath(MenuNavigation.System.ImportLicense.spnLicStatusLS)) .getAttribute("title").toString() + ", Agents Allowed - " + objWebDriver .findElement(By.xpath( MenuNavigation.System.ImportLicense.spnAgntAllowedLS)) .getText().toString()); accessLog .info("Linux WorkStation : Status - " + objWebDriver .findElement(By .xpath(MenuNavigation.System.ImportLicense.spnLicStatusLW)) .getAttribute("title").toString() + ", Agents Allowed - " + objWebDriver .findElement(By.xpath( MenuNavigation.System.ImportLicense.spnAgntAllowedLW)) .getText().toString()); accessLog .info("MAC WorkStation : Status - " + objWebDriver .findElement(By .xpath(MenuNavigation.System.ImportLicense.spnLicStatusMW)) .getAttribute("title").toString() + ", Agents Allowed - " + objWebDriver .findElement(By.xpath( MenuNavigation.System.ImportLicense.spnAgntAllowedMW)) .getText().toString()); } // Take Screenshot CommonFunctions.takeScreenshot(objWebDriver, strCurScriptName, "license_imported", accessLog); result = true; } } else if (objWebDriver.findElement(By.xpath(MenuNavigation.System.ImportLicense.spnLicStatusLW)) .getAttribute("title").toString().equalsIgnoreCase("License Active")) { accessLog.info("the license is already imported, here is the summary :: "); accessLog.info("Windows Server : Status - " + objWebDriver.findElement(By.xpath(MenuNavigation.System.ImportLicense.spnLicStatusWS)) .getAttribute("title").toString() + ", Agents Allowed - " + objWebDriver.findElement(By.xpath(MenuNavigation.System.ImportLicense.spnAgntAllowedWS)) .getText().toString()); accessLog.info("Windows WorkStation : Status - " + objWebDriver.findElement(By.xpath(MenuNavigation.System.ImportLicense.spnLicStatusWW)) .getAttribute("title").toString() + ", Agents Allowed - " + objWebDriver.findElement(By.xpath(MenuNavigation.System.ImportLicense.spnAgntAllowedWW)) .getText().toString()); accessLog.info("Linux Server : Status - " + objWebDriver.findElement(By.xpath(MenuNavigation.System.ImportLicense.spnLicStatusLS)) .getAttribute("title").toString() + ", Agents Allowed - " + objWebDriver.findElement(By.xpath(MenuNavigation.System.ImportLicense.spnAgntAllowedLS)) .getText().toString()); accessLog.info("Linux WorkStation : Status - " + objWebDriver.findElement(By.xpath(MenuNavigation.System.ImportLicense.spnLicStatusLW)) .getAttribute("title").toString() + ", Agents Allowed - " + objWebDriver.findElement(By.xpath(MenuNavigation.System.ImportLicense.spnAgntAllowedLW)) .getText().toString()); accessLog.info("MAC WorkStation : Status - " + objWebDriver.findElement(By.xpath(MenuNavigation.System.ImportLicense.spnLicStatusMW)) .getAttribute("title").toString() + ", Agents Allowed - " + objWebDriver.findElement(By.xpath(MenuNavigation.System.ImportLicense.spnAgntAllowedMW)) .getText().toString()); result = true; } } catch (Exception e) { accessLog.error("Caught Exception while Importing the license "); accessLog.error(e.getMessage()); e.printStackTrace(); result = false; } return result; } @Test(groups = { "ConfigureServer" }) public void importLicense() throws Exception { // login blnStatus = CommonFunctions.login(objWebDriver, accessLog, Driver.strUser, Driver.strPwd); Thread.sleep(1000); // Check if login is successful or not if (blnStatus) { // Login is Successful , hence started with Rule Creation // Take Screenshot try { CommonFunctions.takeScreenshot(objWebDriver, strCurScriptName, "logindone", accessLog); } catch (Exception e) { accessLog.error("Caught Exception while taking the screenshot : " + e.getMessage()); e.printStackTrace(); } // Import license importLicence(objWebDriver, "C:\\Users\\Administrator.QAINDIA\\Documents\\Automation\\Functional_Auto\\data\\DGLicenseFile (Install by 2017-01-01)(import).txt", accessLog); // Logout from DGMC CommonFunctions.logout(objWebDriver, accessLog); // Update the status of test case pass / fail count updateStatus(blnStatus); objReport.write2HTMLfile(sno, strCurScriptName, blnStatus, totalTC, testsPassed); } } } /* * final List<WebElement> iframes = * objWebDriver.findElements(By.tagName("iframe")); System.out.println( * "total number of frames : " + iframes.size()); for (WebElement iframe : * iframes) { System.out.println("frame text : " + iframe.getText()); * System.out.println("frame id : " + iframe.getAttribute("id")); * System.out.println("frame src : " + iframe.getAttribute("src")); * System.out.println("frame name : " + iframe.getAttribute("name")); } */