using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium.Interactions;
/* For using Remote Selenium WebDriver */
using OpenQA.Selenium.Remote;
 
namespace Firefox_Demo
{
    class Firefox_Demo
    {
        String test_url_1 = "https://lambdatest.github.io/sample-todo-app/";
        String test_url_2 = "https://www.lambdatest.com";
        IWebDriver driver;
 
        [SetUp]
        public void start_Browser()
        {
            // Profile details at https://accounts.lambdatest.com/profile
            String username = "user-name";
            String accesskey = "access-key";
            String gridURL = "@hub.lambdatest.com/wd/hub";
 
            // Local Selenium WebDriver
            //driver = new FirefoxDriver();
 
            //Start - Implementation of Remote Selenium WebDriver
            DesiredCapabilities capabilities = new DesiredCapabilities();
 
            capabilities.SetCapability("user", username);
            capabilities.SetCapability("accessKey", accesskey);
            capabilities.SetCapability("build", "Using Firefox WebDriver using C# and NUnit");
            capabilities.SetCapability("name", "Using Firefox WebDriver using C# and NUnit");
            capabilities.SetCapability("platform", "Windows 10");
            capabilities.SetCapability("browserName", "Firefox");
            capabilities.SetCapability("version", "62.0");
 
            driver = new RemoteWebDriver(new Uri("https://" + username + ":" + accesskey + gridURL), capabilities, TimeSpan.FromSeconds(600));
            //End - Implementation of Remote Selenium WebDriver
 
            driver.Manage().Window.Maximize();
 
        }
 
        [Test, Order(1)]
        public void test_ff_1()
        {
            driver.Url = test_url_1;
            String itemName = "Adding item to the list";
 
            System.Threading.Thread.Sleep(2000);
 
            // Click on First Check box
            IWebElement firstCheckBox = driver.FindElement(By.Name("li1"));
            firstCheckBox.Click();
 
            // Click on Second Check box
            IWebElement secondCheckBox = driver.FindElement(By.Name("li2"));
            secondCheckBox.Click();
 
            // Enter Item name
            IWebElement textfield = driver.FindElement(By.Id("sampletodotext"));
            textfield.SendKeys(itemName);
 
            // Click on Add button
            IWebElement addButton = driver.FindElement(By.Id("addbutton"));
            addButton.Click();
 
            // Verified Added Item name
            IWebElement itemtext = driver.FindElement(By.XPath("/html/body/div/div/div/ul/li[6]/span"));
            String getText = itemtext.Text;
 
            // Check if the newly added item is present or not using
            // Condition constraint (Boolean)
            Assert.That((itemName.Contains(getText)), Is.True);
 
            /* Perform wait to check the output */
            System.Threading.Thread.Sleep(2000);
 
            Console.WriteLine("Firefox - Test 1 Passed");
        }
 
        [Test, Order(2)]
        public void test_ff_2()
        {
            driver.Url = test_url_2;
            String hover_xpath = "/html/body/div[2]/section[2]/div/div/div[2]/div/div[1]";
            String learn_more_xpath = "/html/body/div[2]/section[2]/div/div/div[2]/div/div[1]/div[2]/span/a";
            String expected_url_title = "Online Appium and Selenium Automation Testing Tool | Selenium Grid for Web Automation Testing";
 
            System.Threading.Thread.Sleep(2000);
 
            // Verified Added Item name
            //IWebElement itemtext = driver.FindElement(By.XPath("/html/body/div/div/div/ul/li[6]/span"));
 
            IJavaScriptExecutor js = driver as IJavaScriptExecutor;
 
            js.ExecuteScript("window.scrollBy(0,500)");
 
            var wait = new WebDriverWait(driver, new TimeSpan(0, 0, 30));
            var element = wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.XPath(hover_xpath)));
 
            Actions action = new Actions(driver);
            action.MoveToElement(element).Perform();
 
            // Since the element is visible, we should perform a click on the Know More section
 
            var more_element = wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.XPath(learn_more_xpath)));
            more_element.Click();
 
            /* Perform wait to check the output */
            System.Threading.Thread.Sleep(2000);
 
            String url_title = driver.Title;
 
            Assert.That(expected_url_title, Is.EqualTo(url_title));
 
            Console.WriteLine("Firefox - Test 2 Passed");
        }
 
        [TearDown]
        public void close_Browser()
        {
            driver.Quit();
        }
    }
}