andruhovski icon

querySelectorTest

andruhovski | PRO | 09/18/17 08:53:49 AM UTC | 0 ⭐ | 981 👁️ | Never ⏰ | []
C# |

8.53 KB

|

None

|

0 👍

/

0 👎

using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Aspose.Html;
 
namespace querySelectorTest
{
    [TestClass]
    public class UnitTest1
    {
        private readonly HTMLDocument _document =
            new HTMLDocument("http://asposedemo20170904120448.azurewebsites.net/home/CssSelectorTest");
 
        [TestMethod] 
        public void TestMethod01()
        {
            //Class 
            var element = _document.QuerySelector(".intro");
            Assert.AreEqual(element.TagName, "DIV");
        }
 
        [TestMethod]
        public void TestMethod02()
        {
            //Id
            var element = _document.QuerySelector("#Lastname");
            Assert.AreEqual(element.TagName, "SPAN");
        }
 
        [TestMethod]
        public void TestMethod03()
        {
            //class and id
            var collection = _document.QuerySelectorAll(".intro, #Lastname");
            Assert.IsNotNull(collection);
            Assert.AreEqual(2, collection.Length);
            Assert.AreEqual("DIV", collection[0].TagName);
            Assert.AreEqual("SPAN", collection[1].TagName);
            
        }
 
        [TestMethod]
        public void TestMethod04()
        {
            //tag
            var collection = _document.QuerySelectorAll("h1");
            Assert.AreEqual(1,collection.Length);
            Assert.AreEqual("H1",collection[0].TagName);            
        }
 
        [TestMethod]
        public void TestMethod05()
        {            
            var collection = _document.QuerySelectorAll("h1, p");
            Assert.AreEqual(8, collection.Length);
            Assert.AreEqual("H1",collection[0].TagName);
            for (var i = 1; i < 8; i++)
            {
                Assert.AreEqual("P", collection[i].TagName);
            }            
        }
 
        [TestMethod]
        public void TestMethod06()
        {         
            var collection = _document.QuerySelectorAll("div p");
            Assert.AreEqual(4, collection.Length);            
            Assert.IsTrue(collection.All(el=>el.TagName.Equals("P")));
        }
 
        [TestMethod]
        public void TestMethod07()
        {            
            var collection = _document.QuerySelectorAll("div > p");
            Assert.AreEqual(3, collection.Length);
            Assert.IsTrue(collection.All(el => el.TagName.Equals("P")));
            foreach (var element in collection)
            {
                Assert.AreEqual(element.TagName, "P");
            }
 
        }
        [TestMethod]
        public void TestMethod08()
        {            
            var collection = _document.QuerySelectorAll("ul + h3");            
            Assert.AreEqual(1,collection.Length);
            Assert.AreEqual("H3",collection[0].TagName);
        }
        [TestMethod]
        public void TestMethod09()
        {                 
            var collection = _document.QuerySelectorAll("ul ~ table");
            Assert.AreEqual(1, collection.Length);
            Assert.IsTrue(collection.All(el=>el.TagName.Equals("TABLE")));                        
        }
        [TestMethod]
        public void TestMethod10()
        {              
            var collection = _document.QuerySelectorAll("*");
            Assert.AreEqual(71,collection.Length);
        }
        [TestMethod]
        public void TestMethod11()
        {             
            var collection = _document.QuerySelectorAll("[id]");
            Assert.AreEqual(9,collection.Length);
        }
        [TestMethod]
        public void TestMethod12()
        {            
            var collection = _document.QuerySelectorAll("[id=my-Address]");
            Assert.AreEqual(1, collection.Length);
            Assert.AreEqual("P",collection[0].TagName);            
        }
        [TestMethod]
        public void TestMethod13()
        {            
            var collection = _document.QuerySelectorAll("p[title*=beautiful]");
            Assert.AreEqual("P", collection[0].TagName);
            Assert.AreEqual(collection.Length, 1);
        }
        [TestMethod]
        public void TestMethod14()
        {            
            var collection = _document.QuerySelectorAll("[id$=ess]");
            Assert.AreEqual("P",collection[0].TagName);
            Assert.AreEqual(1,collection.Length);
        }
        [TestMethod]
        public void TestMethod15()
        {             
            var collection = _document.QuerySelectorAll("[id|=my]");
            Assert.AreEqual("P",collection[0].TagName, "P");
            Assert.AreEqual(1, collection.Length);
        }
 
        [TestMethod]
        public void TestMethod16()
        {            
            var collection = _document.QuerySelectorAll("[id^=L]");
            Assert.AreEqual(2, collection.Length);
            Assert.AreEqual(collection[0].TagName, "SPAN");
            Assert.AreEqual(collection[1].TagName, "UL");            
        }
 
        [TestMethod]
        public void TestMethod17()
        {            
            //TODO: Improve this test
            var collection = _document.QuerySelectorAll("[title~=beautiful]");            
            Assert.AreEqual(1, collection.Length);
            Assert.AreEqual("P", collection[0].TagName);
        }
        [TestMethod]
        public void TestMethod17A()
        {
            //TODO: Improve this test
            var otherDocument = new HTMLDocument(@"http://asposedemo20170904120448.azurewebsites.net/home/jobposting");                        
            var collection = otherDocument.QuerySelectorAll("[itemtype~=JobPosting]");
            Assert.AreEqual(1, collection.Length);
            Assert.AreEqual("DIV", collection[0].TagName);
        }
 
        [TestMethod]
        public void TestMethod18()
        {            
            var collection = _document.QuerySelectorAll("[id*=s]");
            Assert.AreEqual(3, collection.Length, 3);
            Assert.AreEqual("SPAN",collection[0].TagName, "SPAN");
            Assert.AreEqual("P",collection[1].TagName) ;
            Assert.AreEqual("UL",collection[2].TagName);            
        }
 
        [TestMethod]
        public void TestMethod19()
        {              
            var collection = _document.QuerySelectorAll(":checked");
            Assert.AreEqual(2, collection.Length);
            Assert.AreEqual("INPUT",collection[0].TagName);
            Assert.AreEqual("INPUT",collection[1].TagName);            
        }
 
        [TestMethod]
        public void TestMethod20()
        {
            var collection = _document.QuerySelectorAll(":disabled");
            Assert.AreEqual(1, collection.Length, 1);
            Assert.AreEqual("INPUT", collection[0].TagName);            
        }
 
        [TestMethod]
        public void TestMethod21()
        {         
            var collection = _document.QuerySelectorAll(":enabled");            
            Assert.IsTrue(collection.All(el=>el.TagName.Equals("INPUT")));
            Assert.AreEqual(8, collection.Length);
        }
 
        [TestMethod]
        public void TestMethod22()
        {
            var collection = _document.QuerySelectorAll(":empty");
            Assert.AreEqual(12,collection.Length, 12);
        }
 
        //[TestMethod]
        //public void TestMethod23()
        //{
            
        //    //TODO: IMPROVE THIS TEST
        //    var collection = _document.QuerySelectorAll(":focus");
        //    Assert.AreEqual(0,collection.Length);
        //}
        //[TestMethod]
        //public void TestMethod24()
        //{
        //    //TODO: IMPROVE THIS TEST     
        //    var collection = _document.QuerySelectorAll("p:first-child");
        //    Assert.AreEqual(1, collection.Length);
        //    Assert.AreEqual("P", collection[0].TagName);            
        //}
 
        //[TestMethod]
        //public void TestMethod25()
        //{
        //    //TODO: IMPROVE THIS TEST
        //    var collection = _document.QuerySelectorAll("p:first-letter");
        //    Assert.AreEqual(collection.Length, 0);
        //    Assert.AreEqual("P", collection[0].TagName);            
        //}
 
        //[TestMethod]
        //public void TestMethod26()
        //{
        //    //TODO: IMPROVE THIS TEST      
        //    var collection= _document.QuerySelectorAll("p:first-line");
        //    Assert.AreEqual("P", collection[0].TagName);
        //    Assert.AreEqual(collection.Length, 0);
        //}
    }
}

Comments