Projekt

Obecné

Profil

Stáhnout (6.28 KB) Statistiky
| Větev: | Tag: | Revize:
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
        there were some problems with multiple tests runs*/
54
        [TestMethod]
55
        [DataRow(false, "aaa", "aaa", "aaa", "aaa")]
56
        public void CreateUser(bool isAdmin, string Username, string Name, string Surname, string Password)
57
        {
58
            var driver = new ChromeDriver();
59
            WebDriverWait w = new WebDriverWait(driver, TimeSpan.FromSeconds(5));
60

    
61
            if (!Login(driver)) Assert.Fail("Login failed");
62
            driver.Manage().Window.Maximize();
63
            //Thread.Sleep(1000);
64
            //driver.FindElement(userButton).Click();
65
            driver.Navigate().GoToUrl(usersUrl);
66
            driver.FindElement(addUser).Click();
67
            //popup handling
68
            string currentHandle = driver.CurrentWindowHandle;
69
            ReadOnlyCollection<string> originalHandles = driver.WindowHandles;
70

    
71
            //popup window actions
72
            if (isAdmin)
73
            {
74
                w.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementExists(newAdminBtn)).Click();
75
            }
76
            else w.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementExists(newAnotatorBtn)).Click();
77

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

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

    
87

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

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

    
117

    
118

    
119
        //getting table of users from page for comparison if there is/is not user there should/should not be
120
        private List<string> getTable(IWebDriver driver)
121
        {
122
            List<string> cont = new List<string>();
123
            IList<IWebElement> allElement = driver.FindElements(By.TagName("td"));
124
            Thread.Sleep(500);
125
            foreach (IWebElement element in allElement)
126
            {
127
                cont.Add(element.Text);
128
            }
129
            return cont;
130
        }
131

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