Tuesday, 26 July 2016

Test SVG Graphs Using Selenium WebDriver



At times, during functional testing validating the data in the graphs or charts become crucial to proceed further.On further analysis on dom structure, graph appears to be built on SVG. This complicates the automation testing.

Many a times, testers just leave the particular scenarios as not automatable saying Graphs / images are not automatable. But, that is not right. We'll have one or the other solution to automate everything. Yes! may not be 100 percent.

Sample SVG Graphs: SVG Sample Graphs


Below approach let you know a way to automate SVG graphs using Selenium WebDriver.




Sample 1: SVG Graph within a embed Tag


URL: https://jwatt.org/svg/demos/scripting-across-embed.html

Script :


System.setProperty("webdriver.chrome.driver","C:/pathtofolder/chromedriver.exe");
WebDriver cdrv = new ChromeDriver();

cdrv.get("https://jwatt.org/svg/demos/scripting-across-embed.html");
cdrv.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

//Get the source value of the svg and navigate to the graph
String svgSource = cdrv.findElement(By.id("embed")).getAttribute("src");
cdrv.navigate().to(svgSource);
cdrv.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
//Find the specific element & get the required value.
WebElement svgpart = cdrv.findElement(By.xpath("//*[name()='svg']/*[name()='rect']"));
WebElement svgpart2 = cdrv.findElement(By.xpath("//*[name()='svg']/*[name()='line'][2]"));
System.out.println(svgpart.getAttribute("fill"));
System.out.println(svgpart2.getAttribute("stroke"));
//Navigate back to the actual page.
cdrv.navigate().back();
cdrv.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

cdrv.quit();



Sample 2: SVG Graph directly under div

URL: http://referencewebapp.qaautomation.net/svg.php

Script:
System.setProperty("webdriver.chrome.driver","C:/pathtofolder/chromedriver.exe");
WebDriver cdrv = new ChromeDriver();

cdrv.get("http://referencewebapp.qaautomation.net/svg.php");
cdrv.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

//Find the specific pie div & get the text.
WebElement VioletePartElm = cdrv.findElement(By.cssSelector("#svgchart > div > svg > g:nth-child(4) > g:nth-child(1) > text")); 
System.out.println(VioletePartElm.getText());
cdrv.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

cdrv.quit();

14 comments:

  1. Hello,
    The Article on Test SVG Graphs Using Selenium WebDriver is nice. It give detail information about it .Thanks for Sharing the information on Selenium Web Driver. mobile application testing

    ReplyDelete
  2. Hi Shiva Prakash, I Need your help. I am working with a gradp designed using D3js and i need to write a automation script check whether it touching 'o' value, then i need to raise an alert and also need to send out email with date range when it was '0' value details.

    x-axis: date
    Y-axis: number of devices.

    How can i do that. Please help me out. Its urgent.

    Thanks in advance.
    @Shiva Krishna

    ReplyDelete
    Replies
    1. Hi Shiva Krishna,
      As far as i know even D3.js uses SVG graphs. If i understand your scenario, you want to check at what date the value is '0' for no. of devices.

      In this scenario, you need to loop through all your X-axis(dates) Nodes,
      Within loop get the value of node value. Where ever you find '0' add it to Arraylist.
      Once whole loop is completed send out email with that date list in the ArrayList.

      Delete
    2. Hi Shiva Prakash,

      Thanks for reply. Just seen ur message. I have followed more likely the same steps. For now i have solve the purpose.

      Any further questions, i shall keep you posted.

      Delete
  3. Hi Shiva, Need urgetnt help. Below is my xpath

    //*[local-name()='svg' and namespace-uri()='http://www.w3.org/2000/svg']//*[local-name()='g']// *[name()='rect']
    which returns 3 node in mozilla. But from script noschelement exception. Can you please help me what is the issue.

    String objectTag= driver.findElement(By.xpath("//div[@id='svgpanel']//embed")).getAttribute("src");

    Its printing ip//name/create.svg ..

    ReplyDelete
  4. So when i give driver.navigate.To(objectTag) then i m able to interact with the element. But the problem is i should be able to access it without navigation.

    ReplyDelete
    Replies
    1. You can give a try switching to that element, & write the xpath which provides only single node.
      However, navigating gives you the cleaner approach & navigating back to main page can be done using browser back functionality.

      Delete
  5. I just followed the same post of your's and wrote the css expressions like #chart1 > svg > g:nth-child(1) >g:nth-child(2) > g:nth-child(1) > text and when i execute script i am getting an error like below
    org.openqa.selenium.InvalidSelectorException: invalid selector: Unable to locate an element with the xpath expression #chart1 > svg > g:nth-child(1) >g:nth-child(2) > g:nth-child(1) > text because of the following error: SyntaxError: Failed to execute 'evaluate' on 'Document': The string '#chart1 > svg > g:nth-child(1) >g:nth-child(2) > g:nth-child(1) > text' is not a valid XPath expression. (Session info: chrome=67.0.3396.99).Please suggest me in this area , how to fix this.

    My html hierachy is








    IMW03

    ReplyDelete
    Replies
    1. Hi, The locator used is the 'cssSelector' and not 'Xpath'. Hence you are seeing that error. Just change it to By.xpath to By.cssSelector in your script.

      Delete
  6. Hello I have a new challenge with a web site made with a lot of SVG objectes nested inside other objects. I would like to make you a lot of questions about this. How can I contact you for this?

    ReplyDelete
  7. Hi everyone,
    I want help on automating line chart.
    url : https://www.bseindia.com/sensex/code/16/
    from the chart i want to get all the values coming on tooltip.

    ReplyDelete