commit 64641e817579cd8734727184765e6bda2eb4e3ab
Author: schenkj <schenkj@students.zcu.cz>
Date:   Sun May 15 15:57:35 2022 +0200

    user creation selenium
    
    creating user with selenium (until layout is fixed, multiple tests are hard to debug/execute)

diff --git a/Backend/FrontendTesting/FrontendTesting.csproj b/Backend/FrontendTesting/FrontendTesting.csproj
index cfbb9a0..8667f14 100644
--- a/Backend/FrontendTesting/FrontendTesting.csproj
+++ b/Backend/FrontendTesting/FrontendTesting.csproj
@@ -8,10 +8,12 @@
   </PropertyGroup>
 
   <ItemGroup>
+    <PackageReference Include="DotNetSeleniumExtras.WaitHelpers" Version="3.11.0" />
     <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
     <PackageReference Include="MSTest.TestAdapter" Version="2.2.7" />
     <PackageReference Include="MSTest.TestFramework" Version="2.2.7" />
     <PackageReference Include="coverlet.collector" Version="3.1.0" />
+    <PackageReference Include="Selenium.Support" Version="4.1.0" />
     <PackageReference Include="Selenium.WebDriver" Version="4.1.0" />
     <PackageReference Include="Selenium.WebDriver.ChromeDriver" Version="100.0.4896.6000" />
     <PackageReference Include="Selenium.WebDriver.GeckoDriver.Win64" Version="0.31.0" />
diff --git a/Backend/FrontendTesting/UserManagementTesting.cs b/Backend/FrontendTesting/UserManagementTesting.cs
new file mode 100644
index 0000000..afd9518
--- /dev/null
+++ b/Backend/FrontendTesting/UserManagementTesting.cs
@@ -0,0 +1,148 @@
+﻿using System.Linq;
+using System.Text;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+using OpenQA.Selenium;
+using OpenQA.Selenium.Chrome;
+using OpenQA.Selenium.Firefox;
+
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using System.Threading;
+using System.Collections.ObjectModel;
+using OpenQA.Selenium.Support.UI;
+
+namespace FrontendTesting
+{
+    [TestClass]
+    public class UserManagementTesting
+    {
+
+        //defining variables needed for testing
+        public string url = "http://localhost:3000/login";
+        public string usersUrl = "http://localhost:3000/users";
+        public By usernameField = By.Id("login_username");
+        public By passwordField = By.Id("login_password");
+        public By loginButton = By.XPath("/html/body/div[1]/div/div/div[2]/main/form/div[3]/div/div/div/button");
+        public By userButton = By.XPath("/html/body/div[1]/div/div[1]/ul/li[2]/span");
+        public By addUser = By.XPath("/html/body/div[1]/div/main/button/span");
+        public By newAnotatorBtn = By.XPath("//*[@id='role']/label[1]");
+        public By newAdminBtn = By.XPath("//*[@id='role']/label[2]");
+        public By newUsernameInput = By.Id("username");
+        public By newPasswordInput = By.Id("password");
+        public By newNameInput = By.Id("name");
+        public By newSurnameInput = By.Id("surname");
+        public By newUserBtn = By.XPath("/html/body/div[3]/div/div[2]/div/div[2]/div[2]/form/div[6]/div/div/div/button");
+        //public By userTableRows = By.XPath("/html/body/div[1]/div/main/div/div/div/div/div/div[2]/table/tbody/tr");
+        //public By userTableClmns = By.XPath("/html/body/div[1]/div/main/div/div/div/div/div/div[2]/table/tbody/tr/td");
+
+        public string adminUsername = "admin";
+        public string adminPassword = "admin";
+
+
+
+        /*[DataRow(false, "bbb", "bbb", "bbb", "bbb")]
+        [DataRow(true, "", "ccc", "ccc", "ccc")]
+        [DataRow(false, "", "ddd", "ddd", "ddd")]
+        [DataRow(true, "eee", "", "eee", "eee")]
+        [DataRow(false, "fff", "", "fff", "fff")]
+        [DataRow(true, "ggg", "ggg", "", "ggg")]
+        [DataRow(false, "hhh", "hhh", "", "hhh")]
+        [DataRow(true, "iii", "iii", "iii", "")]
+        [DataRow(false, "jjj", "jjj", "jjj", "")]*/
+        [TestMethod]
+        [DataRow(false, "aaa", "aaa", "aaa", "aaa")]
+        public void CreateUser(bool isAdmin, string Username, string Name, string Surname, string Password)
+        {
+            var driver = new ChromeDriver();
+            WebDriverWait w = new WebDriverWait(driver, TimeSpan.FromSeconds(5));
+            
+            if (!Login(driver)) Assert.Fail("Login failed");
+            driver.Manage().Window.Maximize();
+            //Thread.Sleep(1000);
+            //driver.FindElement(userButton).Click();
+            driver.Navigate().GoToUrl(usersUrl);
+            driver.FindElement(addUser).Click();
+            //popup handling
+            string currentHandle = driver.CurrentWindowHandle;
+            ReadOnlyCollection<string> originalHandles = driver.WindowHandles;
+            
+            //popup window actions
+            if (isAdmin)
+            {
+                w.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementExists(newAdminBtn)).Click();
+            }
+            else w.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementExists(newAnotatorBtn)).Click();
+
+            w.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementExists(newUsernameInput)).SendKeys(Username);
+            w.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementExists(newPasswordInput)).SendKeys(Password);
+            w.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementExists(newNameInput)).SendKeys(Name);
+            w.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementExists(newSurnameInput)).SendKeys(Surname);
+            w.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementExists(newUserBtn)).Click();
+            Thread.Sleep(2000);
+
+            driver.Navigate().Refresh();
+
+            
+            //Here should be new user in database if creditals were valid
+            var table = getTable(driver);
+            bool found = false;
+            string nm, srnm, usrnm, rl, dcks;
+            for(int i = 0; i < table.Count-5; i+=6)
+            {
+                nm= table[i];
+                srnm= table[i+1];
+                usrnm= table[i+2];
+                //rl= table[i+3];
+                //dcks= table[i+4];
+                if (nm == Name && srnm == Surname && usrnm == Username) found = true;
+            }
+            if(Username!="" || Password != "" || Name!="" || Surname!="")
+            {
+                //valid creditals - user should be in db
+                Assert.IsTrue(found, "There should be new user but he was not found.");
+
+            }
+            else
+            {
+                //invalids creditals - user shouldnt be in db
+                Assert.IsFalse(found, "There should NOT be new user but he was found in the table. ");
+            }
+            driver.Navigate().GoToUrl(url);            
+            driver.Close();
+            driver.Quit();
+        }
+
+
+
+
+        private List<string> getTable(IWebDriver driver)
+        {
+            List<string> cont = new List<string>();
+            IList<IWebElement> allElement = driver.FindElements(By.TagName("td"));
+            Thread.Sleep(500);
+            foreach (IWebElement element in allElement)
+            {
+                cont.Add(element.Text);
+            }
+            return cont;
+        }
+
+        private bool Login(IWebDriver driver)
+        {
+            driver.Navigate().GoToUrl(url);
+            Thread.Sleep(3000);
+            driver.FindElement(usernameField).SendKeys(adminUsername);        //enter username
+            Thread.Sleep(1000);
+            driver.FindElement(passwordField).SendKeys(adminPassword);          //enter password
+            Thread.Sleep(1000);
+            driver.FindElement(loginButton).Click();                      //click on login
+            Thread.Sleep(5000);
+            if (!driver.Url.Equals("http://localhost:3000/documents/admin"))
+            {
+                return false;
+            }
+            return true;
+        }
+    }
+}
