Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added acceptance tests #116

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions acceptance-tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Use following steps to run acceptance test locally:

1. Install Docker
2. mvn clean install -Dmaven.test.skip=true
3. Run `docker-compose.yaml` // DO NOT forget to provide absolute path to init-mongo.sh script
4. mvn clean install or run tests manually
61 changes: 61 additions & 0 deletions acceptance-tests/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.inifi</groupId>
<artifactId>unifi-network-tests</artifactId>
<version>1.0.0</version>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
<version>4.24.0</version>
</dependency>

<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.24.0</version>
</dependency>

<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>5.5.0</version>
<scope>test</scope>
</dependency>


<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.10.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.docker-java</groupId>
<artifactId>docker-java</artifactId>
<version>3.2.12</version> <!-- Use the latest stable version -->
</dependency>
</dependencies>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>

<profiles>
<profile>
<id>acceptance-tests</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
</plugin>
</plugins>
</build>
</profile>
</profiles>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.unifi.driver;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import java.time.Duration;

public class BrowserManager {
ChromeOptions options;
WebDriver driver;

public WebDriver create() {
options = new ChromeOptions();
options.addArguments("--ignore-certificate-errors", "--disable-search-engine-choice-screen");
driver = new ChromeDriver(options);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(Duration.ZERO);
return driver;
}

public WebDriver getDriver() {
if (driver == null) {
return create();
}
return driver;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.unifi.driver;

import org.openqa.selenium.WebDriver;

public class DriverProvider {

private static ThreadLocal<WebDriver> drivers = new ThreadLocal<>();

private DriverProvider() {
}

public static WebDriver getDriverInstance() {
if (drivers.get() == null) {
createNewDriver();
}
return drivers.get();
}

public static void closeDriver() {
if (drivers.get() != null) {
drivers.get().quit();
drivers.remove();
}
}

private static void createNewDriver() {
WebDriver driver = new BrowserManager().create();
drivers.set(driver);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.unifi.driver.pages;

import com.unifi.driver.DriverProvider;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;


public class BasePage {

WebDriver driver = DriverProvider.getDriverInstance();

WebDriverWait wait = new WebDriverWait(driver, Duration.ofMillis(30000));

public void clickElement(By by) {
System.out.println("Clicking on element " + by.toString());
wait.until(ExpectedConditions.elementToBeClickable(by)).click();
}

public void focusAndClickElement(By by) {
System.out.println("Focusing the element " + by.toString());
new Actions(driver).moveToElement(wait.until(ExpectedConditions.elementToBeClickable(by))).click().perform();
}

public void sendKeysToElement(String text, By by) {
System.out.println("Sending keys onto element " + by.toString());
wait.until(ExpectedConditions.elementToBeClickable(by)).sendKeys(text);
}

public String getTextFromElement(By by) {
return driver.findElement(by).getText();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.unifi.driver.pages;

import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;

public class DashboardPage extends BasePage{
private static By dashboardButton = By.xpath("//a[@class='component__rHEvxowz icon-light__rHEvxowz active__rHEvxowz']");
private static By topologyButton = By.xpath("//a[@class='component__rHEvxowz icon-light__rHEvxowz active__rHEvxowz']");
private static By textFromActibvityTab = By.xpath("/html[1]/body[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[4]/div[1]/span[1]");
private static By settingsTab = By.xpath("//a[@data-testid='navigation-settings']");
private static By systemTab = By.xpath("//span[@data-testid='system']");
private static By countryField = By.xpath("//div[@class='inputContainer__xvBRiNo6 inputContainer-light-secondary__xvBRiNo6 inputContainer-secondary__xvBRiNo6']");

public void makeSomeActivitiesForAdminChecking(){
clickElement(topologyButton);
clickElement(dashboardButton);
driver.navigate().refresh();
wait.until(ExpectedConditions.visibilityOfElementLocated(textFromActibvityTab));
}

public static By getTextFromActibvityTab() {
return textFromActibvityTab;
}

public String checkCountryCode() {
clickElement(settingsTab);
clickElement(systemTab);
return getTextFromElement(countryField);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.unifi.driver.pages;

import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;

public class InitSetupPage extends BasePage{
private static By termsCheckBox = By.xpath("//input[@id='tosAndEula']");
private static By nextButton = By.cssSelector("button[type='submit'] span[class='content__jTNy2Cxe']");
private static By skipButton = By.xpath("//span[contains(text(),'Skip')]");
private static By createUIAccountButton = By.cssSelector("button[class='button__jTNy2Cxe button-light__jTNy2Cxe secondary__jTNy2Cxe secondary-light__jTNy2Cxe is-accessible__jTNy2Cxe is-accessible-light__jTNy2Cxe medium__jTNy2Cxe'] span[class='content__jTNy2Cxe'] span");
private static By advancedSetupLink = By.xpath("//span[contains(text(),'Advanced Setup')]");
private static String countryCodeText;

private static By countryCode = By.xpath("//*[@id=\"root\"]/div[1]/div[3]/div/form/div/div[2]/div[2]");


public static String getCountryCodeText() {
return countryCodeText;
}

public static By getTermsCheckBox() {
return termsCheckBox;
}

public static By getNextButton() {
return nextButton;
}


public static By getCreateUIAccountButton() {
return createUIAccountButton;
}

public void prepareForSetupAccount(){
countryCodeText = wait.until(ExpectedConditions.presenceOfElementLocated(countryCode)).getText();
clickElement(termsCheckBox);
clickElement(nextButton);
clickElement(advancedSetupLink);
clickElement(skipButton);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.unifi.driver.pages;

import org.openqa.selenium.By;

public class RegisterPage extends BasePage{
private String userName;
private String email;
private String password;

private static By userNameField = By.xpath("//input[@id='localAdminUsername']");
private static By userEmailField = By.xpath("//input[@id='localAdminEmail']");
private static By userPasswordField = By.xpath("//input[@id='localAdminPassword']");
private static By userConfirmPasswordField = By.xpath("//input[@id='localAdminPasswordConfirm']");

private static By finishButton = By.xpath("//span[normalize-space()='Finish']");



public RegisterPage(String userName, String email, String password) {
this.userName = userName;
this.email = email;
this.password = password;
}

public void fillFormsAndFinish () {
sendKeysToElement(this.userName, userNameField);
sendKeysToElement(this.email, userEmailField);
sendKeysToElement(this.password, userPasswordField);
sendKeysToElement(this.password, userConfirmPasswordField);
clickElement(finishButton);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package com.unifi.driver.utils;

import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.StartContainerCmd;
import com.github.dockerjava.api.command.StopContainerCmd;
import com.github.dockerjava.core.DefaultDockerClientConfig;
import com.github.dockerjava.core.DockerClientBuilder;
import com.github.dockerjava.core.DockerClientConfig;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class DockerContainerManager {

private DockerClient dockerClient;

public String getContainerName() {
return containerName;
}

private String containerName = "unifi-network-application";

public DockerContainerManager() {
DockerClientConfig dockerClientConfig = DefaultDockerClientConfig.createDefaultConfigBuilder()
.withDockerHost("tcp://localhost:2375")
.withDockerTlsVerify(false)
.build();
this.dockerClient = DockerClientBuilder.getInstance(dockerClientConfig).build();
}

public void restartContainer(String containerId) {
try {
StopContainerCmd stopCmd = dockerClient.stopContainerCmd(containerId);
stopCmd.exec();
System.out.println("Container " + containerId + " stopped.");

StartContainerCmd startCmd = dockerClient.startContainerCmd(containerId);
startCmd.exec();
System.out.println("Container " + containerId + " started.");
} catch (Exception e) {
e.printStackTrace();
}
}

private String runCommand(String command) {
StringBuilder output = new StringBuilder();
ProcessBuilder processBuilder = new ProcessBuilder();
processBuilder.command("bash", "-c", command);

try {
Process process = processBuilder.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;

while ((line = reader.readLine()) != null) {
output.append(line).append("\n");
}

int exitCode = process.waitFor();
if (exitCode != 0) {
throw new IOException("Error executing command: " + command);
}

} catch (IOException | InterruptedException e) {
System.err.println("Error: " + e.getMessage());
}

return output.toString().trim();
}

public void editFile(String filePath, String searchPattern, String replacement) {
String escapedSearchPattern = searchPattern.replace("/", "\\/");
String escapedReplacement = replacement.replace("/", "\\/");

String command = String.format(
"docker exec %s sed -i 's/%s/%s/g' %s",
containerName, escapedSearchPattern, escapedReplacement, filePath
);
runCommand(command);
}

public void editSystemProperties(){
String filePath = "/usr/lib/unifi/data/system.properties";
String searchPattern = "is_default=false";
String replacement = "is_default=true";
this.editFile(filePath, searchPattern, replacement);
}

}
Loading