phpunit selenium getting header information of the url open - selenium

I am using phpunit and selenium to write some functional testing for my project.
What I am looking for is the way to retrieve Response-header
There are some URLs which dont have a .js extension but return javascript as the code.
What I am looking for is the header information (especially content-type) of the URL opened with $this->open(); function
For eg:
//if I do
$this->open('http://stackoverflow.com');
// or
$this->open('http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js')
// and then use some function like
$this->getResponseHeaders();
// returns me array of header information or
// some function just for content type
$this->getContentType();
// which return text/html or text/javascript
Is there any way selenium provides this functionality or a hack or a trick to get the details. I went through the selenium documentation and the TestCase files and didnt find anything close to it.

It's sad, but you cannot. It does not allowed by phpunit because it does not allowed by WebDriver.
We will not be adding this feature to the WebDriver API as it falls outside of our current
scope (emulating user actions).

If I understand your question you need these two steps:
$this->open('http://stackoverflow.com');
$htmltext = $this->getHtmlSource();
and then you can get the inforamtion by splitting the htmltext...

Related

How to add „From” request header in Firefox-Selenium?

I’m looking for a way to add the “From” request header to disclose an email address of the requesting user.
I am using Selenium with Firefox, alternatively I can switch to PhantomJS or Chrome.
It could be some preference to set in selenium.webdriver.FirefoxProfile. I checked Firefox’s about:config documentation, but can’t find any indication how to implement this header. Any help is appreciated especially that it is difficult to google this issue given the name of the header.
Still haven’t found an interface how to do this with Selenium. There is however a workaround with intercepting the request using selenium-wire:
from seleniumwire import webdriver
def from_request_header_interceptor(request):
del request.headers['From']
request.headers['From'] = 'email#example.com'
driver = webdriver.Firefox(**kwargs)
driver.request_interceptor = from_request_header_interceptor
driver.get('https://www.httpbin.org/headers')
The httpbin outputs i.a. headers “From”: “email#example.com”. Seems to work.

Automate a button click on chrome://extensions page using selenium webdriver

I'm trying to write an automated test that will automate the process of updating a google chrome extension. I'm not aware of another method of doing this automatically so here is what I'm currently trying to do:
Open the chrome extensions page (as far as I'm aware this is just an html page unless I'm missing something).
Click on the "Update extensions" button
Here is what I have tried having opened the chrome extensions page:
IwebElement UpdateButton = driver.findelement(By.Id("update-extensions-now"));
UpdateButton.Click();
For some reason the button click is not registering. I have tried some other locators such as CSS path and Xpath but they don't work either. Also, when I debug this test, it passes fine so I know it's not an issue with any of my locators. I have (as a test) tried to automate clicks on the other elements on this page and it's the same issue. I can't get a handle on any elements on the chrome://extensions page at all.
Has anyone encountered this or have any ideas as to what's going on?
You can use the Chrome extensions API to auto-update required extension.
Find the file "manifest.json" in the default Google Chrome
C:\Users\*UserName*\AppData\Local\Google\Chrome\User Data\Default\Extensions
There find the update URL of your extension:
{
"name": "My extension",
...
"update_url": "http://myhost.com/mytestextension/updates.xml",
...
}
The returned XML by the Google server looks like:
<?xml version='1.0' encoding='UTF-8'?>
<gupdate xmlns='http://www.google.com/update2/response' protocol='2.0'>
<app appid='yourAppID'>
<updatecheck codebase='http://myhost.com/mytestextension/mte_v2.crx' version='2.0' />
</app>
</gupdate>
appid
The extension or app ID, generated based on a hash of the public key, as described in Packaging. You can find the ID of an extension or Chrome App by going to the Extensions page (chrome://extensions).
codebase
A URL to the .crx file.
version
Used by the client to determine whether it should download the .crx file specified by codebase. It should match the value of "version" in the .crx file's manifest.json file.
The update manifest XML file may contain information about multiple extensions by including multiple elements.
Another option is to use the --extensions-update-frequency command-line flag to set a more frequent interval in seconds. For example, to make checks run every 45 seconds, run Google Chrome like this:
chrome.exe --extensions-update-frequency=45
Note that this affects checks for all installed extensions and apps, so consider the bandwidth and server load implications of this. You may want to temporarily uninstall all but the one you are testing with, and should not run with this option turned on during normal browser usage.
The request to update each individual extension would be:
http://test.com/extension_updates.php?x=id%3DyourAppID%26v%3D1.1
You can find even more detailed information on exntesions developers site: https://developer.chrome.com/extensions
If you look at the HTML of the "chrome://extensions" page you will notice that the "Update extensions now" button is contained within an iframe. You need to switch to the iframe before trying to register a button click. i.e:
(This is in c#. Note that this code is written from memory so it may not be 100% accurate. Also, you will want to write more robust method. This code just quickly demonstrates that by switching to the iframe, it will work ok)
String ChromeExtensionsPage = "chrome://extensions";
driver.Navigate().GoToUrl(ChromeExtensionsPage);
driver.Switchto().Frame("DesiredFrame");
IwebElement UpdateButton = driver.findelement(By.Id("DesiredButtonID"));
UpdateButton.Click();

Mink and Sahi timing out on getContent

I've written a website scraper for use in a project.
I'm controlling Firefox through Sahi using Mink to visit each site and interact with any elements where necessary. I've managed to get this working perfectly on all sites I've tried except for one...
I'm trying to get the markup from https://www.o2.co.uk/shop/phones/
I'm using the exact same code for this page, as I have for all others:
// Configure driver
$this->driver = new \Behat\Mink\Driver\SahiDriver('firefox',
new \Behat\SahiClient\Client(
new \Behat\SahiClient\Connection(null, CRAWL_SERVER, 9999)
)
);
// Init session:
$this->session = new \Behat\Mink\Session($this->driver);
// Start session:
$this->session->start();
// Open the url
$this->session->visit($config['url']);
// Get the markup from the page
$markup = $this->session->getPage()->getContent();
When I use this code to attempt to get the markup from https://www.o2.co.uk/shop/phones/ Mink just seems to hang, waiting for something to happen.
It would seem that maybe something on this page is preventing either Sahi or Mink from returning the markup. I've also tried running other functions instead of getContent(), such as $this->session->wait(2000); and attempting to search through getPage using the find command.
If anyone has any idea as to why this is happening I would be very interested in finding out why and how I can make this work.
tl;dr
Why is Mink/Sahi timing out on this site?

Unable to get CSS Selector "nth-type" working with WebDriver

Though have been able to use simple CSS (just including id, name) but "nth-type" gives up.
Here is the test method -
public static String GetTextByCSSSelector(IWebDriver webDriver, String cssLocator)
{
return webDriver.FindElement(By.CssSelector(cssLocator)).Text;
}
and this is the method call -
GetTextByCSSSelector(_webDriver, "css=div#filelist div:nth-child(1)");
and this is the exception -
OpenQA.Selenium.WebDriverException : An invalid or illegal string was specified
I used the locator mentioned above in Selenium IDE and it does highlight the element.
Any guess?
#
On a different note, did you support Selenium Proposal on area51 -
http://area51.stackexchange.com/proposals/4693/selenium
You just need to click Commit button to support site, while having logged using your Google/Yahoo account.
This proposal is backed by SeleniumHQ and we need more users to commit to it to make it see day of light.
#
Try div:first-child.
If that works it would imply that you only have CSS2 ability, not CSS3 ability.
Remove css= from selector, and enjoy :)

Selenium Test - Check if url returns a pdf

I have a selenium test I am creating. The application creates a pdf for the user. I want to know how to test that a url returns a pdf file. I don't really care what is in the pdf, just that one got created.
FYI - The url does not container .pdf
http://www.url.com/printp/something.php?pld=b8eiub9w6ar2am1i
Alternatively, make a HEAD request first to check if the url is created, then inspect content_length header, which should be nonzero size, or whatever is reasonable minimum size in your case.
Can you test if //meta[contains(#http-equiv,Content-Type)][contains(#content,pdf)] exists!
I did not test !