I am new to selenium, I have a selenium script that works fine for chrome, I could add the the extension to the chrome using the .crx file but how can I use the extension while testing with selenium. For Instance I have 'Resource Override' extension which replaces one of my javascript file with the custom file. I cannot open inspect element and trigger the extension as chrome driver does not allow us to do so.
1.I am not sure how can I get the extension to use my javascript file using selenium. The extension has a text field to provide a path to the file that needs to be replaced and and a text box to add custom javascript code.
2.Is it possible to dynamically have the application use my custom javascript file using selenium with out using mentioned extension. I want to find alternatives with out checking in my code.
System.setProperty("webdriver.chrome.driver","/Path/chromedriver");
System.setProperty("webdriver.chrome.logfile", "/Path/sel_logs.log");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
LoggingPreferences loggingprefs = new LoggingPreferences();
loggingprefs.enable(LogType.BROWSER, Level.ALL);
capabilities.setCapability(CapabilityType.LOGGING_PREFS, loggingprefs);
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File("/Path/0.6.2_0.crx"));
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new ChromeDriver(capabilities);
driver.get("https://google.com");
Related
Here is my code:
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js">
</script>
<script>
$(function() {
var thing = [];
var bar = $.getJSON('C:\Users\cccompro\foo.json', function(obj) {
for (i = 0; i < obj.length; i++) {
thing.push(obj[i]);
}
});
});
</script>
I'm not sure why it doesn't work. "foo.json" contains an array of objects.
If you are trying the code at Question at Chrome or Chromium browsers, launch the browser instance with --allow-file-access-from-files flag set. Note that open instances of Chrome or Chromium should be closed when you launch the browser or the instance will be launched with the open browser instances' configuration folder, instead of with the flag set. You can launch Chrome or Chromium with an existing instance open and honoring the flag by using --user-data-dir flag with value set a different directory than open instance of Chrome or Chromium.
Technically, it is also possible to write to user file system without using an extension with window.webkitRequestFileSystem. Though using chrome.fileSystem within an extension provides an API designed to achieve the read/write.
See
Jquery load() only working in firefox?
Read local XML with JS
How to Write in file (user directory) using JavaScript?
How to use webkitRequestFileSystem at file: protocol
JavaScript/Ajax Write to File
Using <input type="file"> element
How to print all the txt files inside a folder using java script
You cannot read files directly from the users hard drive without the browsers permission. This would be a huge security issue if you could even though there are ways to allow this (checkout guests answer).
You could however try to make the user select the file and then read it with Javascript.
This is called the HTML 5 file API.
However, this doesn't work for any browser and you probably have to use a server anyway in this case.
For more information on this checkout this or this post.
I would like to write simple scripts which after I have already opened site ( I dont wanna script to open it) press two buttons and insert data in comment section after pressing f.ex. 'g' button. I am completly new in that kind of programming so any help will be nice( also link to good tutorials).
webBrowser1.Document.GetElementById("User").SetAttribute("value", textBox1.Text);
webBrowser1.Document.GetElementById("but").InvokeMember("click");
I am aware of those 2 functions i will use but how to instantiate them on already opened page by pressing a button? (If thats important deafult used browser is opera).
You should use something like Selenium (http://www.seleniumhq.org/) which is a browser automation framework.
Selenium scripts can be written in many languages (including c#) and the scripts can be run on a variety of browsers. There is even browser plugins for creating scripts my recording a macro - no code required!
This is much more robust that using a browser control embedded in an app as that is only a cut down version of internet explorer I believe.
This is a rough sample of selenium in c#
using OpenQA.Selenium;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Support.UI;
var options = new InternetExplorerOptions();
options.IntroduceInstabilityByIgnoringProtectedModeSettings = true;
Driver = new InternetExplorerDriver(options);
Driver.Navigate().GoToUrl("yourURL");
Driver.FindElement(By.Id("User")).SendKeys("<your text>");
Driver.FindElement(By.Id("but")).Click();
I'm trying to turn off javascript via the profile when opening using Selenium. This has work previously but now I've updated Selenium/Firfox I can't get it to work.
profile = webdriver.FirefoxProfile()
profile.set_preference('javascript.enabled', False)
driver = webdriver.Firefox(profile)
driver.implicitly_wait(30)
driver.get("http://www.enable-javascript.com/")
All other settings seem to change while using profile.set_preference() on other option and javascript.enabled exists and is set to True when I look at the Firefox settings about:config. Is it possible Javascript is being set to True after loading the profile or something?
FF version 43.0.3
Selenium version 2.48.0
Any suggestions on why this could be happening?
UPDATE
Adding profile.add_extension("path/to/noscript_security_suite-2.9.xpi"); to the above code with the downloaded extension as #alecxe suggested fixed the issue.
This issue affects selenium starting with 2.46.0, javascript.enabled is being ignored:
Firefox driver 2.46.0 regression - unable to set to non-js
As a workaround, load the noscript addon, see:
How to disable Javascript when using Selenium by JAVA?
I'm using selenium for web test by JAVA.
I want to stop JavaScript on Firefox Browser,Google Chrome Browser,IE Browser.
I tried this code on Firefox Browser.
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("javascript.enabled", false);
WebDriver driver = new FirefoxDriver(profile);
But it's return this error on second line.
Exception in thread "main" java.lang.IllegalArgumentException: Preference javascript.enabled may not be overridden: frozen value=true, requested value=false
How to disable Javascript when using Selenium each Browser?
If you know about this problem,Please help me!
Use noscript addon for firefox. Right click on the Add to Firefox button and Save link as that will give you the option to save the .xpi. Then, configure the the Firefox profiler as follows.
FirefoxProfile profile = new FirefoxProfile();
profile.AddExtension(#"PATH\TO\noScript.xpi");
IWebDriver driver = new FirefoxDriver(profile);
driver.Navigate().GoToUrl("http://localhost:8080/");
usually selenium does not identify elements on a browser toolbar. But this is for pre-build browser toolbars. Can a browser toolbar ( for FF, IE & Chrome ) be developed in such a way that selenium can be used to identify elements on the toolbar and then perform control actions on it , like clicking on a radio button on the toolbar.
Because Selenium can only control things that JavaScript can control, you need to find out whether JavaScript can control the toolbar. JavaScript has 3 main features. I controls the JavaScript processing, the DOM (document object model) and the BOM (browser object model). There might have been more control given to JavaScript when HTML 5 and CS3 came out, but this is my knowledge of it.
I think you may need to use a cross-browser extension to make your sidebar/toolbar. google-gears/silverlight/adobe-air/jnext. You might need to make the UI using the DOM. I'm not sure. You will need to research.. never done this before.
If the toolbar is part of the BOM (and/or DOM), then you can. Just create a custom command, called Selenium.prototype.doControlToolbar or something and put your JavaScript logic inside of that function (similar to what I have below). Good luck!
Option #1 - if using Selenium IDE:
Specify the user-extensions.js file under Selenium IDE > Options (menu) > Options (menu option) > General Tab, then browse to your file under "Selenium Core Extensions".
Option #2 - if using Selenium RC Server:
If you're not using the IDE and using Selenium RC server with a client driver (like JUnit for example), you must specify the path of the *.js file with the -userExtensions parameter when you start the Selenium RC Server on the command line. But you said you just wanted to use the IDE, so I'd ignore this. It takes quite a bit of other setup to use the Selenium RC server.
java -jar selenium-server.jar -userExtensions user-extensions.js
=======================
I made the following custom command (JavaScript function) in my custom user-extensions.js file.. I had to exit and restart the IDE before it found it. Type everything after the "do" in the "Command" field in the IDE to find the custom command. It looks like it also added a "customAlertAndWait" to the IDE as well.
Code in user-extensions.js file:
Selenium.prototype.doCustomAlert = function(sTarget, sValue) { alert('Target: ' + sTarget + ' ... Value: ' + sValue); };
Selenium IDE command details:
Command: customAlert
Target: custom alert target
Value: custom alert value