Mime Type for adding sauce labs video link in cucumber report - selenium

I am trying to add a video link to the cucumber report using embedded method in scenario ,
String html = "www.saucelabs.com/asdfs234234sdafs/video.mp4";
scenario.embed(html.getBytes(), "text/html");
In the report i still see as text , not a link , also tried below , now this opening in windows media player instead of in browser
String html = "https://www.saucelabs.com/asdfs234234sdafs/video.mp4" >VideoLink;
scenario.embed(html.getBytes(), "text/html");
Can you please help me ? i want a link to appeared in the report by clicking upon , it must navigate to the sauce labs link.
Thanks in advance.

Ruby Implementation: (You can use the same logic in other languages too).
Just call this below line in your hooks.rb, before closing the driver. The below line of code will build the saucelabs video link dynamically based on driver session id.
puts "<button style=\"background-color: #525fa0;font-weight: bold;color: #fff;border: double;border-radius: 25px;\" onclick= \"window.open('https://saucelabs.com/jobs/#{browser.driver.session_id}','_blank')\">SauceLabs Execution Report</button>"
This will add a button to the html report with the saucelabs video link.
If you want to pass the url then use the below code.
puts "<button style=\"background-color: #525fa0;font-weight: bold;color: #fff;border: double;border-radius: 25px;\" onclick= \"window.open(url_goes_here,'_blank')\">SauceLabs Execution Report</button>"
If you are trying to access the html report from jenkins then make sure to run the below line in jenkins console.
System.setProperty("hudson.model.DirectoryBrowserSupport.CSP", "sandbox allow-scripts allow-popups unsafe-inline; ")
Make sure to restart the jenkins, once you ran the script in the script console.
This will allow you to see the report in the html otherwise your url will be blocked.

Related

How to create a better screenshot during the selenium automation in JAVA?

I am creating automated test cases by using selenium 3 & testng. Everything looks good, except the screenshots that are generated. Here is my piece of code to create screenshots PNG files:
file = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(file, new File(pngfile));
which is pretty standard way to do it, but the quality of the created PNG file is not so good.
As you can see from following PNG file. In the picture, the email value ( "....#yahoo.com"), which should be at the upper-right corner of the web-page and should be as high as the other navigation bar elements on the left side. But in the created PNG file, this item has been squeezed to the lower level, which is not what I am looking for. Any ideas ? Thanks for the help !
Make sure your window is the right size when you're opening the browser. You can do this via visual inspection or using Selenium's getSize method. I assume you're using Java, but here it is in Python as well.
Then, if the window is not of the correct size in order to guarantee that your webpage's CSS doesn't break, use setSize. Here is that method in Python as well.
Afterwards, your screenshot should look like the window does.
Please try this,
public void calltakeScreenShot(String SSName) throws Exception
{
File screen = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
BufferedImage img = ImageIO.read(screen);
File filetest = Paths.get(".").toAbsolutePath().normalize().toFile();
ImageIO.write(img, "png", new File(filetest + "\\Screenshots\\" + SSName + ".png"));
}
Additionally Here you just need to pre-created Screenshots folder in your Project directory. It will store it by getting absolute path of your project. Also you can manage screenshot name by passing argument.
After several day's research, here is my latest summary.
A). If I was executing the scripts, that is not in the "headless" mode. When the selenium test case is being executed, I will see a new browser session is being popped up and get to that URL, click some buttons, etc, ... till the execution is finished. In this execution, the screenshot page will be saved in good quality.
B). For the same selenium test script, if I am including one extra ChromeOptions setting, which is "--headless", I will not see any browser being brought up during the execution. And once execution is finished, I will get the screenshot with such squeezed web elements.
Comments ?

org.openqa.selenium.WebDriverException: unknown error: cannot focus element on trying to upload a file in selenium

I am trying to upload a file in the webpage through selenium webdriver, and i have written the below code
driver.findElement(By.cssSelector("#collapseDocuments > div > button.button.ng-isolatescope")).sendKeys("C:\\Users\\siva.247588\\Desktop\\Clarient+AML+Questionnaire.docx");
The CSS selector is that of the browse button.
I am getting the below error whwn i run this:
Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: cannot focus element
What is wrong with my code? Could any one advise?
When you try to upload a file, the control is shifted to our local machine from website where we were writing scripts.
So normal click method and select method won't work. You can use robot method to browse and upload some file from your local machine to websites.
Hope this help you..
See the below link for help:
http://www.seleniumeasy.com/selenium-tutorials/webdriver-file-upload-using-robots
I've used this somewhere , hope you can relate this:
Robot rb = new Robot();
rb.keyPress(KeyEvent.VK_A);
rb.keyRelease(KeyEvent.VK_A);
rb.keyPress(KeyEvent.VK_L);
rb.keyRelease(KeyEvent.VK_L);
rb.keyPress(KeyEvent.VK_E);
rb.keyRelease(KeyEvent.VK_E);
rb.keyPress(KeyEvent.VK_R);
rb.keyRelease(KeyEvent.VK_R);
rb.keyPress(KeyEvent.VK_T);
rb.keyRelease(KeyEvent.VK_T);
Thread.sleep(4000L);
rb.keyPress(KeyEvent.VK_TAB);
rb.keyRelease(KeyEvent.VK_TAB);
Thread.sleep(4000L);
rb.keyPress(KeyEvent.VK_ENTER);
rb.keyRelease(KeyEvent.VK_ENTER);
This will browse and upload a file with name "Alert"
If you need more help, kindly provide me code and what you want to upload , I'll help you to resolve your issue.
As #theroot mentioned AutoIT is a good solution, but the downside is it works only on windows based platforms. The best solution so far that I have come across for upload or focus outside browser situations is using Sikuli. Example code is below
Screen sikuliObject= new Screen(); // Creating Screen class object
Pattern add = new Pattern("path to the image.png"); // Path of the upload button image.
sikuliObject.click(add); // Click on Upload button
Thread.sleep(2000); // Wait for 2 seconds for upload window pop-up.
sikuliObject.type("Path of the uploading file and click on enter."+ Key.ENTER );
//Path of the uploading file and click on enter.
Thread.sleep(2000); // Wait for 2 seconds to load the file.
I've once had a similar problem (I'm not sure anymore if it threw a "not visible" exception or the "could not focus"one) and the problem was that the input Element wasn't accessible because it had no dimensions. I've changed the DOM information using the JavaScriptExecuter and afterwards it worked fine. Just go through your page with Firebug and look if the element is really visible.

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();

ReportViewer cannot shown in Firefox8 and Chrome15

I have an asp.net mvc3 project, it has some reports in aspx web pages. Everything works great in 2-3 weeks ago, but now, when I open the reports pages in FireFox8 and Chrome 15, the reports can not be displayed correctly.
In FF8, report content can be shown, but looks like css doesn't loaded, and report head control look like this:
In Chrome15, report head looks fine but content isn't shown up. I can see it has correct contents when using Firebug to view those .
Since data can ben loaded to browser, I think it maybe a browser compatibility problem, am I right?
Does anyone seeing same issues? I'm open to any suggestions.
BTW:My report viewer's version is 10.0.0.0.
Thanks.
[Edit in 12/21/2011]
FireFox
After several research on this, I have noticed when browse in firefox, server will catch an [Microsoft.Reporting.WebForms.HttpHandlerInputException: Missing URL parameter: Name], and the query string is OpType=BackImage&Version=10.0.40219.329&Color=%23ECE9D8&Name=Microsoft.Reporting.WebForms.Icons.toolbar_bk.png; path_info is Reserved.ReportViewerWebControl.axd
This may be the reason why firefox can't show page with correct CSS.
I don't know why this happens in Firefox & Chrome, while in IE everything is fine.
I've found this link shows same excpetion: http://social.msdn.microsoft.com/Forums/eu/vsreportcontrols/thread/80ce3ee8-e65b-476c-b668-ecc8e83ea73d
But I'm not sure how did he solve this, can you or somebody else gives some tip or example code to solve this?
Chrome
When browse in chrome, I found the report data is loaded into DOM, and surronded by a with id="ReportViewer1_fixedTable". Inside that element, there's a div with id="ReportViewer1_ctl09" nested in , if I remove the "height: 100%", the report can be seen.
this link http://www.apptools.com/examples/tableheight.php, explained why this 100% cannot be rendered correctly, but I'm still failed when trying to give a fixed height via CSS, the code can't be pasted here, please see this as a reference.
Thanks.
In Chrome, it's the "height=100%" problem.
Once I add following jQuery code, the report area can ben shown.
$(function () {
$("#ReportViewer1_ctl09").css("height", "auto");
});
In FireFox, the report page raise an error as following:
"Microsoft.Reporting.WebForms.HttpHandlerInputException 缺少 URL 参数: Name"
and the Query String is: "OpType=BackImage&Version=10.0.40219.329&Color=%23ECE9D8&Name=Microsoft.Reporting.WebForms.Icons.toolbar_bk.png"
according to this article, I think it's caused by a proxy that handles http request..
I checked my request, there's a "FiftyOne ..." in "Brwoser" property of "Request" ojbect.
Since fifty one is no longer used in this project, I removed it from Bin and web config, then the report can be shown in FireFox!
Hope this article can be usefule to other guys.

How should I test Gmaps4rails generated maps?

I am using gmaps4rails-1.3.0 to render a few maps in my rails-3.0.7 application. When the map loads, it already has a few markers set. The users get to select which type of markers they want to see by getting to click on a few checkboxes. This works fine in all versions of FF, Chrome and IE that I have checked.
So, now I wanted to write automated test cases to check this functionality. For this, I wrote rspec-2.6.0 request examples which are run through capybara-1.1.2 using the selenium-2.14.0 driver. The test is able to load the map fine with all of the markers showing up correctly. However, no markers are removed when the test unchecks one of the checkboxes. I inserted a debugger statement at the end of test and then checked the error console in the browser. It had the following message:
console is not defined
http://127.0.0.1:51643/javascripts/gmaps4rails/gmaps4rails.googlemaps.js?1323052026 Line 196
Checking the relevant file showed the following function:
Gmaps4RailsGoogle.prototype.clearMarker = function(marker) {
console.log(marker);
return marker.serviceObject.setMap(null);
};
I then noticed that during normal operation, a no. of such log messages are displayed in the firebug console. The selenium webdriver profile doesn't have firebug and nor do I expect all my users to have firebug.
Should a console.log() call be there in any javascript file post-development? If yes, what's the way to test it with a rendering driver, in particular with selenium?