5

I'm trying to do an assert to make sure a correct XMLHttpRequest is made. I know that I can get the exact Request URL from the Developers Console -> Network tab -> XHR tab's general information section. Currently, I'm using Eclipse with TestNG and JAVA as my programming language.

http://example.crowdynews.com/crowdynews/usa/politics/ 

Above is the site I'm testing, and what I need to verify (with AssertTrue) is the Request URL for the "Republican" option in the drop down menu located at the top left of the screen. I'm not sure how to retrieve that specific URL.

I've spent an hour online trying to find a solution for you, but to no avail. I noticed that JMeter did get mentioned a bit in some of the links I read. If anyone knows how to do this or could point me to the documentation I could read that would help, I'd be very appreciative.

  • Are you using JMeter for functional tests? Or you're using JMeter API from tests written in Java and with TestNG library? – dzieciou Dec 13 '15 at 7:09
  • 1
    Please clarify, are you looking for a solution in JMeter? or Selenium + TestNG? They are different tools (for different purposes). Please clarify your current setup with requirements. – TestingWithArif Dec 13 '15 at 7:50
  • @TestingWithArif I'm working with TestNG + Selenium. I only mentioned JMeter since I saw it coming up during my searches. – Tscott Dec 13 '15 at 16:12
  • @dzieciou Neither? I only mentioned it because it came up in my searches while I was looking for a solution. – Tscott Dec 13 '15 at 16:13
  • 1
    @TestingWithArif, actually my solution is based on exporting HAR, programmatically, from Web driver session. – dzieciou Dec 14 '15 at 20:02
3

I can see multiple solutions:

  1. Parse HTML page you test, e.g., with some XML+XPath+JSoup libraries, and check whether element you selected has a right link. This will not work with XHR/AJAX requests, that are sent with JavaScript.
  2. Click the element you want, e.g., with Selenium Web Driver, and capture the HTTP traffic with HTTP proxy, e.g., BrowserMob Proxy. This would work with XHR/AJAX requests.
  3. Variation of the above, but you're not checking links in XHR requests, you just check the right information has appeared on the page, e.g., related to Republicans. All this can be done with Selenium Web Driver.
  4. Finally, you may test correctness of JavaScript only, e.g., that executing certain JavaScript function a correct XmlHttpRequest is generated before sending it.

I will elaborate on 2nd solution as seems to match your actual needs. Here's a sketch of the solution:

ProxyServer proxy = new ProxyServer();
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.PROXY, proxy);
driver = new FirefoxDriver(capabilities);
proxy.newHar("test");
driver.get("http://example.crowdynews.com/crowdynews/usa/politics/");
driver.findElement(By.linkText("Republicans")).click();
Har har = proxy.getHar();
final String expectedUrl = "http://statistics.crowdynews.com/bb?d=crowdynews&s=us-politics-demo&a=topic_rep‌​ublicans&t=1449977726129";
assertThat(har.getLog().getEntries().get(2).getRequest().getUrl(), is(equalTo(expectedUrl)));

If you need more I suggest you browsing through for BrowserMob Proxy tests in GitHub repository.

  • well I already have what the URL is ---> http://statistics.crowdynews.com/bb?d=crowdynews&s=us-politics-demo&a=topic_republicans&t=1449977726129 . What I'm trying to do is use an Assert to make sure that URL is correct when the Republican option is selected. I can't use the current URL in the address bar considering the site it dynamic and never changes. – Tscott Dec 13 '15 at 16:14
  • So, go for solution 2. – dzieciou Dec 13 '15 at 21:59
  • Could you elaborate a little more on #2? How would I actually capture the HTTP traffic with Selenium/TestNG only? – Tscott Dec 14 '15 at 0:03
  • @Tscott Actually, you can't do it with Selenium/TestNG only, you need some HTTP proxy library: see the updated answer. I haven't tried the solution but it still can be a good starting point. – dzieciou Dec 14 '15 at 20:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.