Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 64641e81

Přidáno uživatelem Jakub Schenk před více než 2 roky(ů)

user creation selenium

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

Zobrazit rozdíly:

Backend/FrontendTesting/FrontendTesting.csproj
8 8
  </PropertyGroup>
9 9

  
10 10
  <ItemGroup>
11
    <PackageReference Include="DotNetSeleniumExtras.WaitHelpers" Version="3.11.0" />
11 12
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
12 13
    <PackageReference Include="MSTest.TestAdapter" Version="2.2.7" />
13 14
    <PackageReference Include="MSTest.TestFramework" Version="2.2.7" />
14 15
    <PackageReference Include="coverlet.collector" Version="3.1.0" />
16
    <PackageReference Include="Selenium.Support" Version="4.1.0" />
15 17
    <PackageReference Include="Selenium.WebDriver" Version="4.1.0" />
16 18
    <PackageReference Include="Selenium.WebDriver.ChromeDriver" Version="100.0.4896.6000" />
17 19
    <PackageReference Include="Selenium.WebDriver.GeckoDriver.Win64" Version="0.31.0" />
Backend/FrontendTesting/UserManagementTesting.cs
1
using System.Linq;
2
using System.Text;
3
using Microsoft.VisualStudio.TestTools.UnitTesting;
4
using OpenQA.Selenium;
5
using OpenQA.Selenium.Chrome;
6
using OpenQA.Selenium.Firefox;
7

  
8
using System;
9
using System.Collections;
10
using System.Collections.Generic;
11
using System.Threading;
12
using System.Collections.ObjectModel;
13
using OpenQA.Selenium.Support.UI;
14

  
15
namespace FrontendTesting
16
{
17
    [TestClass]
18
    public class UserManagementTesting
19
    {
20

  
21
        //defining variables needed for testing
22
        public string url = "http://localhost:3000/login";
23
        public string usersUrl = "http://localhost:3000/users";
24
        public By usernameField = By.Id("login_username");
25
        public By passwordField = By.Id("login_password");
26
        public By loginButton = By.XPath("/html/body/div[1]/div/div/div[2]/main/form/div[3]/div/div/div/button");
27
        public By userButton = By.XPath("/html/body/div[1]/div/div[1]/ul/li[2]/span");
28
        public By addUser = By.XPath("/html/body/div[1]/div/main/button/span");
29
        public By newAnotatorBtn = By.XPath("//*[@id='role']/label[1]");
30
        public By newAdminBtn = By.XPath("//*[@id='role']/label[2]");
31
        public By newUsernameInput = By.Id("username");
32
        public By newPasswordInput = By.Id("password");
33
        public By newNameInput = By.Id("name");
34
        public By newSurnameInput = By.Id("surname");
35
        public By newUserBtn = By.XPath("/html/body/div[3]/div/div[2]/div/div[2]/div[2]/form/div[6]/div/div/div/button");
36
        //public By userTableRows = By.XPath("/html/body/div[1]/div/main/div/div/div/div/div/div[2]/table/tbody/tr");
37
        //public By userTableClmns = By.XPath("/html/body/div[1]/div/main/div/div/div/div/div/div[2]/table/tbody/tr/td");
38

  
39
        public string adminUsername = "admin";
40
        public string adminPassword = "admin";
41

  
42

  
43

  
44
        /*[DataRow(false, "bbb", "bbb", "bbb", "bbb")]
45
        [DataRow(true, "", "ccc", "ccc", "ccc")]
46
        [DataRow(false, "", "ddd", "ddd", "ddd")]
47
        [DataRow(true, "eee", "", "eee", "eee")]
48
        [DataRow(false, "fff", "", "fff", "fff")]
49
        [DataRow(true, "ggg", "ggg", "", "ggg")]
50
        [DataRow(false, "hhh", "hhh", "", "hhh")]
51
        [DataRow(true, "iii", "iii", "iii", "")]
52
        [DataRow(false, "jjj", "jjj", "jjj", "")]*/
53
        [TestMethod]
54
        [DataRow(false, "aaa", "aaa", "aaa", "aaa")]
55
        public void CreateUser(bool isAdmin, string Username, string Name, string Surname, string Password)
56
        {
57
            var driver = new ChromeDriver();
58
            WebDriverWait w = new WebDriverWait(driver, TimeSpan.FromSeconds(5));
59
            
60
            if (!Login(driver)) Assert.Fail("Login failed");
61
            driver.Manage().Window.Maximize();
62
            //Thread.Sleep(1000);
63
            //driver.FindElement(userButton).Click();
64
            driver.Navigate().GoToUrl(usersUrl);
65
            driver.FindElement(addUser).Click();
66
            //popup handling
67
            string currentHandle = driver.CurrentWindowHandle;
68
            ReadOnlyCollection<string> originalHandles = driver.WindowHandles;
69
            
70
            //popup window actions
71
            if (isAdmin)
72
            {
73
                w.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementExists(newAdminBtn)).Click();
74
            }
75
            else w.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementExists(newAnotatorBtn)).Click();
76

  
77
            w.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementExists(newUsernameInput)).SendKeys(Username);
78
            w.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementExists(newPasswordInput)).SendKeys(Password);
79
            w.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementExists(newNameInput)).SendKeys(Name);
80
            w.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementExists(newSurnameInput)).SendKeys(Surname);
81
            w.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementExists(newUserBtn)).Click();
82
            Thread.Sleep(2000);
83

  
84
            driver.Navigate().Refresh();
85

  
86
            
87
            //Here should be new user in database if creditals were valid
88
            var table = getTable(driver);
89
            bool found = false;
90
            string nm, srnm, usrnm, rl, dcks;
91
            for(int i = 0; i < table.Count-5; i+=6)
92
            {
93
                nm= table[i];
94
                srnm= table[i+1];
95
                usrnm= table[i+2];
96
                //rl= table[i+3];
97
                //dcks= table[i+4];
98
                if (nm == Name && srnm == Surname && usrnm == Username) found = true;
99
            }
100
            if(Username!="" || Password != "" || Name!="" || Surname!="")
101
            {
102
                //valid creditals - user should be in db
103
                Assert.IsTrue(found, "There should be new user but he was not found.");
104

  
105
            }
106
            else
107
            {
108
                //invalids creditals - user shouldnt be in db
109
                Assert.IsFalse(found, "There should NOT be new user but he was found in the table. ");
110
            }
111
            driver.Navigate().GoToUrl(url);            
112
            driver.Close();
113
            driver.Quit();
114
        }
115

  
116

  
117

  
118

  
119
        private List<string> getTable(IWebDriver driver)
120
        {
121
            List<string> cont = new List<string>();
122
            IList<IWebElement> allElement = driver.FindElements(By.TagName("td"));
123
            Thread.Sleep(500);
124
            foreach (IWebElement element in allElement)
125
            {
126
                cont.Add(element.Text);
127
            }
128
            return cont;
129
        }
130

  
131
        private bool Login(IWebDriver driver)
132
        {
133
            driver.Navigate().GoToUrl(url);
134
            Thread.Sleep(3000);
135
            driver.FindElement(usernameField).SendKeys(adminUsername);        //enter username
136
            Thread.Sleep(1000);
137
            driver.FindElement(passwordField).SendKeys(adminPassword);          //enter password
138
            Thread.Sleep(1000);
139
            driver.FindElement(loginButton).Click();                      //click on login
140
            Thread.Sleep(5000);
141
            if (!driver.Url.Equals("http://localhost:3000/documents/admin"))
142
            {
143
                return false;
144
            }
145
            return true;
146
        }
147
    }
148
}

Také k dispozici: Unified diff