From a1b94d541533c095dc39e7a79c3a776286b09969 Mon Sep 17 00:00:00 2001 From: Menaka Madushanka Date: Thu, 10 Mar 2016 15:05:44 +0530 Subject: [PATCH] Code cleanup and refactoring --- .../ui/pages/devices/DevicesPage.java | 28 ++++---- .../ui/pages/home/IOTAdminDashboard.java | 3 +- .../ui/pages/home/IOTHomePage.java | 5 +- .../integration/ui/pages/login/LoginPage.java | 33 ++++++--- .../src/main/resources/mapper.properties | 13 ++++ .../integration/web/ui/test/Constants.java | 20 ++++++ ...Test.java => LoginFormValidationTest.java} | 54 +++++++++----- .../test/user/AddUserFormValidationTest.java | 72 +++++++++++++------ .../src/test/resources/testng.xml | 17 ++--- 9 files changed, 172 insertions(+), 73 deletions(-) rename modules/integration/tests-iot-web-ui/src/test/java/org/wso2/carbon/iot/integration/web/ui/test/login/{LoginFailTest.java => LoginFormValidationTest.java} (64%) diff --git a/modules/integration/tests-common/web-ui-pages/src/main/java/org/wso2/iot/integration/ui/pages/devices/DevicesPage.java b/modules/integration/tests-common/web-ui-pages/src/main/java/org/wso2/iot/integration/ui/pages/devices/DevicesPage.java index d8e7d918..3b145c88 100644 --- a/modules/integration/tests-common/web-ui-pages/src/main/java/org/wso2/iot/integration/ui/pages/devices/DevicesPage.java +++ b/modules/integration/tests-common/web-ui-pages/src/main/java/org/wso2/iot/integration/ui/pages/devices/DevicesPage.java @@ -32,9 +32,9 @@ import java.io.IOException; import java.util.List; public class DevicesPage { + Log log = LogFactory.getLog(DevicesPage.class); private WebDriver driver; private UIElementMapper uiElementMapper; - Log log = LogFactory.getLog(DevicesPage.class); public DevicesPage(WebDriver driver) throws IOException { @@ -60,24 +60,26 @@ public class DevicesPage { public ConnectedCupDeviceViewPage viewDevice(String deviceName) throws IOException { WebElement deviceTable = driver.findElement(By.xpath(uiElementMapper.getElement("iot.devices.table.xpath"))); - List data = deviceTable.findElements(By.cssSelector("a")); - for (WebElement e : data) { - String s = getLink(e, "/device/connectedcup?id="); - if (s != null) { - driver.get(s); - return new ConnectedCupDeviceViewPage(driver, deviceName); - } + List data = deviceTable.findElements(By.cssSelector("a")); + for (WebElement e : data) { + String s = getLink(e, "/device/connectedcup?id="); + if (s != null) { + driver.get(s); + return new ConnectedCupDeviceViewPage(driver, deviceName); } + } return null; } - public String getLink(WebElement element, String lookupText) { + private String getLink(WebElement element, String... lookupText) { String link = element.getAttribute("href"); log.info("Link -----------------------> " + link); - if (link.contains(lookupText)) { - log.info("returned ----------------->>>> " + link); - return link; + boolean check = true; + for (String s : lookupText) { + if (!link.contains(s)) { + check = false; + } } - return null; + return check ? link : null; } } diff --git a/modules/integration/tests-common/web-ui-pages/src/main/java/org/wso2/iot/integration/ui/pages/home/IOTAdminDashboard.java b/modules/integration/tests-common/web-ui-pages/src/main/java/org/wso2/iot/integration/ui/pages/home/IOTAdminDashboard.java index e448c786..6d84d06e 100644 --- a/modules/integration/tests-common/web-ui-pages/src/main/java/org/wso2/iot/integration/ui/pages/home/IOTAdminDashboard.java +++ b/modules/integration/tests-common/web-ui-pages/src/main/java/org/wso2/iot/integration/ui/pages/home/IOTAdminDashboard.java @@ -33,7 +33,6 @@ import org.wso2.iot.integration.ui.pages.uesr.UserListingPage; import java.io.IOException; - /** * This class represents the Admin Dashboard of the IOT server. * Server dashboard has following sections and functions. @@ -69,7 +68,7 @@ public class IOTAdminDashboard { } public LoginPage logout() throws IOException { - driver.findElement(By.xpath("/html/body/header/div/div[2]/a/span[1]")).click(); + driver.findElement(By.xpath(uiElementMapper.getElement("iot.user.registered.name"))).click(); WebElement logout = driver.findElement(By.xpath(uiElementMapper.getElement("iot.user.logout.link.xpath"))); logout.click(); return new LoginPage(driver); diff --git a/modules/integration/tests-common/web-ui-pages/src/main/java/org/wso2/iot/integration/ui/pages/home/IOTHomePage.java b/modules/integration/tests-common/web-ui-pages/src/main/java/org/wso2/iot/integration/ui/pages/home/IOTHomePage.java index b8697a94..484ab614 100644 --- a/modules/integration/tests-common/web-ui-pages/src/main/java/org/wso2/iot/integration/ui/pages/home/IOTHomePage.java +++ b/modules/integration/tests-common/web-ui-pages/src/main/java/org/wso2/iot/integration/ui/pages/home/IOTHomePage.java @@ -53,7 +53,10 @@ public class IOTHomePage { return name.contains(uiElementMapper.getElement("iot.user.login.username")); } - //To logout + + /** + * Perform the logout action. + * */ public LoginPage logout() throws IOException { driver.findElement(By.xpath(uiElementMapper.getElement("iot.user.registered.name"))).click(); WebElement logout = driver.findElement(By.xpath(uiElementMapper.getElement("iot.user.logout.link.xpath"))); diff --git a/modules/integration/tests-common/web-ui-pages/src/main/java/org/wso2/iot/integration/ui/pages/login/LoginPage.java b/modules/integration/tests-common/web-ui-pages/src/main/java/org/wso2/iot/integration/ui/pages/login/LoginPage.java index c5a051dc..69b65639 100644 --- a/modules/integration/tests-common/web-ui-pages/src/main/java/org/wso2/iot/integration/ui/pages/login/LoginPage.java +++ b/modules/integration/tests-common/web-ui-pages/src/main/java/org/wso2/iot/integration/ui/pages/login/LoginPage.java @@ -40,6 +40,9 @@ public class LoginPage { private static final Log log = LogFactory.getLog(LoginPage.class); private WebDriver driver; private UIElementMapper uiElementMapper; + private WebElement userNameField; + private WebElement passwordField; + private WebElement loginButton; public LoginPage(WebDriver driver) throws IOException { this.driver = driver; @@ -49,6 +52,11 @@ public class LoginPage { if (!webDriverWait.until(ExpectedConditions.titleContains("Login | IoT Server"))) { throw new IllegalStateException("This is not the Login page"); } + userNameField = driver.findElement(By.xpath( + uiElementMapper.getElement("iot.user.login.input.username.xpath"))); + passwordField = driver.findElement(By.xpath( + uiElementMapper.getElement("iot.user.login.input.password.xpath"))); + loginButton = driver.findElement(By.xpath(uiElementMapper.getElement("iot.user.login.button.xpath"))); } /** @@ -60,13 +68,9 @@ public class LoginPage { */ public IOTHomePage loginAsUser(String username, String password) throws IOException { log.info("Login as " + username); - WebElement userNameField = driver.findElement(By.xpath( - uiElementMapper.getElement("iot.user.login.input.username.xpath"))); - WebElement passwordField = driver.findElement(By.xpath( - uiElementMapper.getElement("iot.user.login.input.password.xpath"))); userNameField.sendKeys(username); passwordField.sendKeys(password); - driver.findElement(By.xpath(uiElementMapper.getElement("iot.user.login.button.xpath"))).click(); + loginButton.click(); return new IOTHomePage(driver); } @@ -79,13 +83,9 @@ public class LoginPage { */ public IOTAdminDashboard loginAsAdmin(String username, String password) throws IOException { log.info("Login as " + username); - WebElement userNameField = driver.findElement(By.xpath( - uiElementMapper.getElement("iot.user.login.input.username.xpath"))); - WebElement passwordField = driver.findElement(By.xpath( - uiElementMapper.getElement("iot.user.login.input.password.xpath"))); userNameField.sendKeys(username); passwordField.sendKeys(password); - driver.findElement(By.xpath(uiElementMapper.getElement("iot.user.login.button.xpath"))).click(); + loginButton.click(); return new IOTAdminDashboard(driver); } @@ -100,4 +100,17 @@ public class LoginPage { registerLink.click(); return new NewUserRegisterPage(driver); } + + public void validateForm(String username, String password) { + WebDriverWait wait = new WebDriverWait(driver, UIConstants.webDriverTimeOut); + wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath( + uiElementMapper.getElement("iot.user.login.input.username.xpath")))); + wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath( + uiElementMapper.getElement("iot.user.login.input.password.xpath")))); + userNameField.clear(); + passwordField.clear(); + userNameField.sendKeys(username); + passwordField.sendKeys(password); + loginButton.click(); + } } diff --git a/modules/integration/tests-common/web-ui-pages/src/main/resources/mapper.properties b/modules/integration/tests-common/web-ui-pages/src/main/resources/mapper.properties index fa42406b..6e370329 100644 --- a/modules/integration/tests-common/web-ui-pages/src/main/resources/mapper.properties +++ b/modules/integration/tests-common/web-ui-pages/src/main/resources/mapper.properties @@ -153,9 +153,22 @@ iot.sample.orderCoffee.xpath=//*[@id="order-cup"] iot.sample.coffee.level.xpath=//*[@id="amount_coffee"] iot.sample.temperature.xpath=//*[@id="amount_temp"] +iot.sample.analytics.link.xpath=//*[@id="device_statistics"]/a + +iot.sample.coffeelevel.graph.path=//*[@id="chart-Coffee Level"]/svg/path[1] +iot.sample.temperature.graph.path=//*[@id="chart-Temperature"]/svg/path[1] + + iot.try.devices.div.classname=//div[@class='try-device-container'] iot.try.device.text.xpath=//p[contains(@class,'try-device-text')] + +#------------------ Graphs (Device View) ------------------------------------------------------------------------------- +iot.stats.graph.container.xpath=//*[@id="div-chart"] +iot.stat.graph.wrapper.xpath=//*[contains(@class, "chartWrapper")] +iot.stat.graph.xAxis.xpath=//*[contains(@class, "custom_x_axis")] +iot.stat.graph.class.name=custom_rickshaw_graph rickshaw_graph + #----------------------------------------------------------------------------------------------------------------------- #iot.user.delete.button=//*[@id="inosh"]/td[5]/a[3] #iot.user.delete.button.confirm=remove-user-yes-link diff --git a/modules/integration/tests-iot-web-ui/src/test/java/org/wso2/carbon/iot/integration/web/ui/test/Constants.java b/modules/integration/tests-iot-web-ui/src/test/java/org/wso2/carbon/iot/integration/web/ui/test/Constants.java index 67cfdc6a..be8ac386 100644 --- a/modules/integration/tests-iot-web-ui/src/test/java/org/wso2/carbon/iot/integration/web/ui/test/Constants.java +++ b/modules/integration/tests-iot-web-ui/src/test/java/org/wso2/carbon/iot/integration/web/ui/test/Constants.java @@ -27,6 +27,26 @@ public class Constants { public static final String IOT_DEVICES_URL = "/devicemgt/devices"; public static final String IOT_CONNECTED_CUP_NAME = "testDevice"; + public static final int IOT_RESTART_THREAD_TIMEOUT = 30; + + public static final String IOT_CONNECTED_CUP_LEVEl = "34"; + public static final String IOT_CONNECTED_CUP_TEMPERATURE = "53"; + + public static final String IOT_GRAPH_X_AXIS = "x"; + public static final String IOT_GRAPH_Y_AXIS = "y"; + + public static final String IOT_CONNECTED_CUP_TEMPERATURE_ID = "Temperature"; + public static final String IOT_CONNECTED_CUP_TEMPERATURE_LEGEND = "Temperature"; + public static final String IOT_CONNECTED_CUP_TEMPERATURE_GRAPH_ID = "temperature"; + public static final String IOT_CONNECTED_CUP_TEMPERATURE_Y_AXIS = "Temperature"; + public static final String IOT_CONNECTED_CUP_TEMPERATURE_X_AXIS = "time"; + + public static final String IOT_CONNECTED_CUP_COFFEE_LEVEL_ID = "Coffee Level"; + public static final String IOT_CONNECTED_CUP_COFFEE_LEVEL_LEGEND = "Coffee Level"; + public static final String IOT_CONNECTED_CUP_COFFEE_LEVEL_GRAPH_ID = "coffeelevel"; + public static final String IOT_CONNECTED_CUP_COFFEE_LEVEL_Y_AXIS = "Coffeelevel"; + public static final String IOT_CONNECTED_CUP_COFFEE_LEVEL_X_AXIS = "time"; + } diff --git a/modules/integration/tests-iot-web-ui/src/test/java/org/wso2/carbon/iot/integration/web/ui/test/login/LoginFailTest.java b/modules/integration/tests-iot-web-ui/src/test/java/org/wso2/carbon/iot/integration/web/ui/test/login/LoginFormValidationTest.java similarity index 64% rename from modules/integration/tests-iot-web-ui/src/test/java/org/wso2/carbon/iot/integration/web/ui/test/login/LoginFailTest.java rename to modules/integration/tests-iot-web-ui/src/test/java/org/wso2/carbon/iot/integration/web/ui/test/login/LoginFormValidationTest.java index 745f1080..f02e8a7a 100644 --- a/modules/integration/tests-iot-web-ui/src/test/java/org/wso2/carbon/iot/integration/web/ui/test/login/LoginFailTest.java +++ b/modules/integration/tests-iot-web-ui/src/test/java/org/wso2/carbon/iot/integration/web/ui/test/login/LoginFormValidationTest.java @@ -28,6 +28,11 @@ import org.wso2.carbon.automation.extensions.selenium.BrowserManager; import org.wso2.carbon.iot.integration.web.ui.test.Constants; import org.wso2.iot.integration.ui.pages.IOTIntegrationUIBaseTestCase; import org.wso2.iot.integration.ui.pages.UIElementMapper; +import org.wso2.iot.integration.ui.pages.home.IOTAdminDashboard; +import org.wso2.iot.integration.ui.pages.login.LoginPage; + +import javax.xml.xpath.XPathExpressionException; +import java.io.IOException; /** @@ -37,7 +42,7 @@ import org.wso2.iot.integration.ui.pages.UIElementMapper; * 2. Incorrect username or password * 3. short password */ -public class LoginFailTest extends IOTIntegrationUIBaseTestCase { +public class LoginFormValidationTest extends IOTIntegrationUIBaseTestCase { private WebDriver driver; private UIElementMapper uiElementMapper; @@ -57,53 +62,66 @@ public class LoginFailTest extends IOTIntegrationUIBaseTestCase { public void emptyLoginFormTest() throws Exception { userNameField.sendKeys(""); passwordField.sendKeys(""); - loginButton.click(); - Assert.assertEquals(driver.findElement(By.id(uiElementMapper.getElement("iot.user.login.username.error"))). - getText(), "Please enter a username"); - Assert.assertEquals(driver.findElement(By.id(uiElementMapper.getElement("iot.user.login.password.error"))). - getText(), "Please provide a password"); + WebElement alertUserName = driver.findElement(By.id( + uiElementMapper.getElement("iot.user.login.username.error"))); + WebElement alertPassword = driver.findElement(By.id( + uiElementMapper.getElement("iot.user.login.password.error"))); + + if (!alertUserName.isDisplayed()) Assert.assertTrue(false, "Alert for user name is not present."); + if (!alertPassword.isDisplayed()) Assert.assertTrue(false, "Alert for password is not present."); + + Assert.assertEquals(alertUserName.getText(), "Please enter a username"); + Assert.assertEquals(alertPassword.getText(), "Please provide a password"); + } @Test(description = "Test for incorrect username") public void incorrectUserNameTest() throws Exception { clearForm(); - userNameField.sendKeys("admin1"); passwordField.sendKeys(automationContext.getSuperTenant().getTenantAdmin().getPassword()); - loginButton.click(); - Assert.assertEquals(driver.findElement(By.xpath(uiElementMapper.getElement("iot.user.login.incorrect.xpath"))). - getText(), "Incorrect username or password.!"); + WebElement alert = driver.findElement(By.xpath(uiElementMapper.getElement("iot.user.login.incorrect.xpath"))); + if (alert.isDisplayed()) { + Assert.assertEquals(alert.getText(), "Incorrect username or password.!"); + } else { + Assert.assertTrue(false, "Alert is not present."); + } + } @Test(description = "Test for incorrect password") public void incorrectPasswordTest() throws Exception { clearForm(); - userNameField.sendKeys(automationContext.getSuperTenant().getTenantAdmin().getPassword()); passwordField.sendKeys("admnn"); - loginButton.click(); - Assert.assertEquals(driver.findElement(By.xpath(uiElementMapper.getElement("iot.user.login.incorrect.xpath"))). - getText(), "Incorrect username or password.!"); + WebElement alert = driver.findElement(By.xpath(uiElementMapper.getElement("iot.user.login.incorrect.xpath"))); + if (alert.isDisplayed()) { + Assert.assertEquals(alert.getText(), "Incorrect username or password.!"); + } else { + Assert.assertTrue(false, "Alert is not present."); + } } @Test(description = "Test for short password") public void shortPasswordTest() throws Exception { clearForm(); - userNameField.sendKeys(automationContext.getSuperTenant().getTenantAdmin().getUserName()); passwordField.sendKeys("ad"); - loginButton.click(); - Assert.assertEquals(driver.findElement(By.id(uiElementMapper.getElement("iot.user.login.password.error"))). - getText(), "Your password must be at least 3 characters long"); + WebElement alert = driver.findElement(By.id(uiElementMapper.getElement("iot.user.login.password.error"))); + if (alert.isDisplayed()) { + Assert.assertEquals(alert.getText(), "Your password must be at least 3 characters long"); + } else { + Assert.assertTrue(false, "Alert is not present."); + } } public void clearForm() throws Exception { diff --git a/modules/integration/tests-iot-web-ui/src/test/java/org/wso2/carbon/iot/integration/web/ui/test/user/AddUserFormValidationTest.java b/modules/integration/tests-iot-web-ui/src/test/java/org/wso2/carbon/iot/integration/web/ui/test/user/AddUserFormValidationTest.java index db6bc291..91e5008d 100644 --- a/modules/integration/tests-iot-web-ui/src/test/java/org/wso2/carbon/iot/integration/web/ui/test/user/AddUserFormValidationTest.java +++ b/modules/integration/tests-iot-web-ui/src/test/java/org/wso2/carbon/iot/integration/web/ui/test/user/AddUserFormValidationTest.java @@ -35,7 +35,7 @@ import javax.xml.xpath.XPathExpressionException; import java.io.IOException; /** - * Test cases for + * Class contains test cases for Add user form validation. * 1. Empty form submission * 2. Short user name * 3. Empty First Name @@ -47,7 +47,6 @@ public class AddUserFormValidationTest extends IOTIntegrationUIBaseTestCase { private WebDriver driver; private UIElementMapper uiElementMapper; - private WebElement firstNameField; private WebElement lastNameField; private WebElement emailField; @@ -71,7 +70,7 @@ public class AddUserFormValidationTest extends IOTIntegrationUIBaseTestCase { } @Test(description = "Test for empty form submission") - public void emptyFormTest(){ + public void emptyFormTest() { clearForm(); firstNameField.sendKeys(""); @@ -81,9 +80,14 @@ public class AddUserFormValidationTest extends IOTIntegrationUIBaseTestCase { addUserButton.click(); - Assert.assertEquals(driver.findElement(By.xpath( - uiElementMapper.getElement("iot.admin.addUser.formError.xpath"))).getText(), - "Username is a required field. It cannot be empty."); + WebElement alert = driver.findElement(By.xpath( + uiElementMapper.getElement("iot.admin.addUser.formError.xpath"))); + + if (!alert.isDisplayed()) { + Assert.assertTrue(false, "Alert for empty form not is displayed."); + } + + Assert.assertEquals(alert.getText(), "Username is a required field. It cannot be empty."); } @Test(description = "Test for short user name") @@ -97,9 +101,14 @@ public class AddUserFormValidationTest extends IOTIntegrationUIBaseTestCase { addUserButton.click(); - Assert.assertEquals(driver.findElement(By.xpath( - uiElementMapper.getElement("iot.admin.addUser.formError.xpath"))).getText(), - "Username must be between 3 and 30 characters long."); + WebElement alert = driver.findElement(By.xpath( + uiElementMapper.getElement("iot.admin.addUser.formError.xpath"))); + + if (!alert.isDisplayed()) { + Assert.assertTrue(false, "Alert for short user name is not displayed."); + } + + Assert.assertEquals(alert.getText(), "Username must be between 3 and 30 characters long."); } @Test(description = "Test for empty first name") @@ -113,9 +122,14 @@ public class AddUserFormValidationTest extends IOTIntegrationUIBaseTestCase { addUserButton.click(); - Assert.assertEquals(driver.findElement(By.xpath( - uiElementMapper.getElement("iot.admin.addUser.formError.xpath"))).getText(), - "Firstname is a required field. It cannot be empty."); + WebElement alert = driver.findElement(By.xpath( + uiElementMapper.getElement("iot.admin.addUser.formError.xpath"))); + + if (!alert.isDisplayed()) { + Assert.assertTrue(false, "Alert for First name is not displayed."); + } + + Assert.assertEquals(alert.getText(), "Firstname is a required field. It cannot be empty."); } @Test(description = "Test for empty last name") @@ -128,9 +142,15 @@ public class AddUserFormValidationTest extends IOTIntegrationUIBaseTestCase { userNameField.sendKeys("user1"); addUserButton.click(); - Assert.assertEquals(driver.findElement(By.xpath( - uiElementMapper.getElement("iot.admin.addUser.formError.xpath"))).getText(), - "Lastname is a required field. It cannot be empty."); + + WebElement alert = driver.findElement(By.xpath( + uiElementMapper.getElement("iot.admin.addUser.formError.xpath"))); + + if (!alert.isDisplayed()) { + Assert.assertTrue(false, "Alert for Last name is not displayed."); + } + + Assert.assertEquals(alert.getText(), "Lastname is a required field. It cannot be empty."); } @Test(description = "Test for empty email name") @@ -144,9 +164,14 @@ public class AddUserFormValidationTest extends IOTIntegrationUIBaseTestCase { addUserButton.click(); - Assert.assertEquals(driver.findElement(By.xpath( - uiElementMapper.getElement("iot.admin.addUser.formError.xpath"))).getText(), - "Email is a required field. It cannot be empty."); + WebElement alert = driver.findElement(By.xpath( + uiElementMapper.getElement("iot.admin.addUser.formError.xpath"))); + + if (!alert.isDisplayed()) { + Assert.assertTrue(false, "Alert for E-mail is not displayed."); + } + + Assert.assertEquals(alert.getText(), "Email is a required field. It cannot be empty."); } @Test(description = "Test for incorrect email") @@ -160,9 +185,14 @@ public class AddUserFormValidationTest extends IOTIntegrationUIBaseTestCase { addUserButton.click(); - Assert.assertEquals(driver.findElement(By.xpath( - uiElementMapper.getElement("iot.admin.addUser.formError.xpath"))).getText(), - "Provided email is invalid. Please check."); + WebElement alert = driver.findElement(By.xpath( + uiElementMapper.getElement("iot.admin.addUser.formError.xpath"))); + + if (!alert.isDisplayed()) { + Assert.assertTrue(false, "Alert for incorrect E-mail is not displayed."); + } + + Assert.assertEquals(alert.getText(), "Provided email is invalid. Please check."); } private void clearForm() { diff --git a/modules/integration/tests-iot-web-ui/src/test/resources/testng.xml b/modules/integration/tests-iot-web-ui/src/test/resources/testng.xml index a9e541d4..2188561b 100644 --- a/modules/integration/tests-iot-web-ui/src/test/resources/testng.xml +++ b/modules/integration/tests-iot-web-ui/src/test/resources/testng.xml @@ -26,18 +26,19 @@ - - - - - - - - + + + + + + + + +