Checkout our demo site to practice selenium https://magento.softwaretestingboard.com/

0 like 0 dislike
659 views
by Contributing Tester (92 points)
edited by

JavaScript not working with selenium C# implementation

 

using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;
using System;

namespace Test.TestSuite
{
    [TestFixture]
    [Category("test")]
    public class Test
    {
        public IWebDriver driver { get; set; }
        string listingUrl = "http://google.com";
        [OneTimeSetUp]
        public void setUp()
        {
            //driver = new ChromeDriver();
            //driver = new InternetExplorerDriver();
            driver = new FirefoxDriver();
            driver.Manage().Window.Maximize();
        }

        [Test]
        public void test_CounterTest()
        {
            driver.Navigate().GoToUrl(listingUrl);
            WaitForElementVisible(By.CssSelector("#asterixv4"));  
            driver.FindElement(By.CssSelector("#ngvi_desc_div > div > div > div.aucCounter"));
            Assert.IsTrue(IsElementPresent(By.CssSelector("#ngvi_desc_div > div > div > div.aucCounter")), "not found on page");
        }

        [Test]
        public void test_ScrollingGallery()
        {
            driver.Navigate().GoToUrl("https://google.com");
            var listingTitleUsed = "Neat";
            driver.Navigate().GoToUrl(listingUrl);
            WaitForElementVisible(By.CssSelector("#but_v4")); 
            IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
            var script = "document.getElementById('csgGallery').style.display='block';";
            js.ExecuteScript(script);
            WaitForElementVisible(By.CssSelector("div[class=csgTitle]"));
            var listingName = getSudoElementContent("div[class=csgTitle]", ":after");
            listingName = listingName.Replace("/", "");
            listingName = listingName.Replace("\"", "");
            Assert.AreEqual(listingTitleUsed, listingName);
        }

        [OneTimeTearDown]
        public void tearDown()
        {
            driver.Quit();
        }

        public string getSudoElementContent(string selector, string cssEvent)
        {
            IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
            string script = String.Format("return window.getComputedStyle(document.querySelector('{0}'),'{1}').getPropertyValue('content')", selector, cssEvent);
            string content = (String)js.ExecuteScript(script);
            return content;
        }

        public void WaitForElementVisible(By element)
        {
            var attempts = 0;
            while (attempts < 2)
            {
                try
                {
                    WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(90));
                    wait.Until(ExpectedConditions.ElementIsVisible(element));
                    break;
                }
                catch (WebDriverException)
                {
                    attempts++;
                }
                catch (InvalidOperationException)
                {
                    attempts++;
                }
            }
            if (attempts >= 2)
            {
                throw new NoSuchElementException("Cannot locate " + element.ToString());
            }
        }

        public bool IsElementPresent(By by)
        {
            try
            {
                driver.FindElement(by);
                return true;
            }
            catch (WebDriverTimeoutException)
            {
                return false;
            }
            catch (NoSuchElementException)
            {
                return false;
            }
            catch (WebDriverException)
            {
                return false;
            }
        }
    }
}

Getting error

 

"Package Selenium.WebDriver 3.3.0 is not compatible with netcoreapp1.1 (.NETCoreApp,Version=v1.1). Package Selenium.WebDriver 3.3.0 supports:
- net35 (.NETFramework,Version=v3.5)
- net40 (.NETFramework,Version=v4.0)
One or more packages are incompatible with .NETCoreApp,Version=v1.1."

1 Answer

0 like 0 dislike
by The go-to Tester (181 points)
It looks like you have to update your .net version. Currently you are using lower version of .Net framework. You should be using atleast .net framework 3.5.

You can download latest .Net framework here:

https://www.microsoft.com/en-in/download/details.aspx?id=17851


This site is for software testing professionals, where you can ask all your questions and get answers from 1300+ masters of the profession. Click here to submit yours now!

...