Manual actions in Selenium - java

started the test in Selenium in addition in my browser I manually clicked and/or entered the data in the fields.
Is it possible to save the actions that I made manually - actions logs?
I want to know what the user's actions during manual test.

I suggest you using Selenium with third-party framework like Robotframework.
It will be easier to observe actions with those behavior driven test framework.
And they will also help you to capture screenshot while error occurs.

maybe you can use this code for screenshot :
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com/");
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
// Now you can do whatever you need to do with it, for example copy somewhere
FileUtils.copyFile(scrFile, new File("c:\\tmp\\screenshot.png"));

SÅ‚awomir, why don't You start record all actions from beginning, after launch webbrowser. For FF55+ there is an addon Katalon Recorder. You can record all steps and than export all actions to Java, Pyton code. Than just copy code from exporter, and You can use it in your webdriver tests.

Below example
Simple example
During the test Selenium opens a page example.com. Next Selenium click on link to example.com/login. On the login page Selenium enters username and password -- it is correct test
Example with manual actions
During the test Selenium opens a page example.com. Now manually I click on link example.com/about (but on About page isn't link to example.com/login Next Selenium try to click on link to example.com/login, but can't because link to example.com/login doesn't exist. -- test failed
Test failed because I make manual action, so I want to log all manually actions

Related

How to take screenshot and then click on that saved screenshot in selenium?

I am unable to click on button on page so I am taking screenshot and trying to click on that screenshot. I need code to take screenshot of particular element and click on that element through code.
Selenium is used to automate only web applications you will not able to perform any operation outside the web application.
Try to use Extent report tool for selenium.
If your test fails(Or any condition you set), It call method for taking screenshot and save it to extent report.
At the end of the test, open extent report html file.

How to save file by Selenium from download pop-up box?

I have done a fair amount of googling and Selenium does not seem to natively support the clicking of the Save button in pop-up box. However I see a workaround which sets the browser preferences in this question -- Access to file download dialog in Firefox
The code given there is as below --
FirefoxProfile firefoxProfile = new FirefoxProfile();
firefoxProfile.setPreference("browser.download.folderList",2);
firefoxProfile.setPreference("browser.download.manager.showWhenStarting",false);
firefoxProfile.setPreference("browser.download.dir","c:\\downloads");
firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk","text/csv");
WebDriver driver = new FirefoxDriver(firefoxProfile);//new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);
driver.navigate().to("http://www.myfile.com/hey.csv");
Unfortunately, I cannot navigate to the file location, shown in the line below, since it redirects me to the login page of the website.
driver.navigate().to("http://www.myfile.com/hey.csv")
I have already logged-in to the website and log-in again does not make sense. Even for that I have to open a new window handler and switch to the new window(I don't think it will work anyway, since after login it directs to the previous page from which the pop-up occurs ). Is there some workaround to avoid this
driver.navigate().to
and somehow click on the Save button on the pop-up dialogue box?
Note1: When I manually open my browser, The pop-up window does not appear at all. The file starts to download when I click on the previous link even when I don't change firefox preferences manually to automatically download the file.
Note2: I Changed the Firefox preferences manually to automatically download the file types (XML in my case), but Selenium does not pick up this preference(expected behavior, I guess).
Simply put, it's not possible to interact with anything that's not part of the DOM. You can't make Selenium click on the "Save" button of the browser.
Most of the time it's not a good idea to download a file anyway (at least not when you're just doing UI testing).
Depending on what exactly you want to do with the file I'd suggest finding another way to test that behavior.

Handle save as dialog box by selenium web driver in java

I'm Trying to save a Html Page using Selenium script in java which perform following operations
open the web page.
fire a right click event.
click on save as html option.
Now problem is when script click on save as html option it shows a pop-up to save html.
I already set default option for download in Firefox but when i run the script it keep showing me pop-up again and again what i need is:-
when Firefox shows a save as dialog box then it'll fire an enter event so that it can save html by using Selenium script.
Robot class in java might help you. Check this link as well.
Here is a sample code:
// Create object of Robot class<br>
Robot object=new Robot();
// Press Enter<br>
object.keyPress(KeyEvent.VK_ENTER);
// Release Enter<br>
object.keyRelease(KeyEvent.VK_ENTER);
Window pop ups cant be handled by Selenium.So you can go for follwing options : -
(1) Java Robot Class
(2) Sikuli [Recommended]
(3) AutoIt
These are 3rd party tools used to handle window based pop ups.
Accepting confirmation popups in Selenium can be handled via Alert object (assuming driver instance is ready):
Alert myAlert = driver.switchTo().alert();
myAlert.accept();
Use autoit
download the autoit exe
record clicking of that button
Use the code and execute it via JS executor
Dont forget to place the code before you are actually executing the step
not after executing it
Please switch to windows dialogs first using get window handles eg. Save As
Then, use Robot commands to send Enter key.
It will save the file.

Using pre opened window with Selenium

I am trying to get Selenium WebDriver to find and use a browser window that is already open before the script executes. I am writting in Java.
I am using selenium-server 2.37.0 and the browser is IE8. I am open to using the Chrome browser as well.
Anyway, I have tried opening a driver instance and then looking for the window handles in the usual way (Set handles = driver.getWindowHandles();) but this only finds the hadle of the window that the driver opened. I have also just tried to switchTo the window by the window cannot be found.
The reason I want to use a pre-opened window is because when I execute my script, for some reason the browser won't let it click a link (It may be because the link is to an https address and sends a username and a token). When the script finishes the webpage won't respond to me manually clicking the link either.
I am able to manualy navigate to the link and click it the link works fine, so my thinking is that I can navigate to the page that I want and then kick of the scripts from there, but I need the webdriver to use this browser window that I used.
I cannot navigate the to link directly with Selenium because the link resided behind a secure server. I have to log in first then click the link and this is where I am having the problem.
Unfortunately, as of 2.37.1 (December 2013), it cannot be done.
There is an official feature request for this in the Selenium project (and it's even the most starred one), but it has not been done yet.
You can identify a browser window by windowHandle and switch between several Windows while testing.
You can print out all existing windowHandles (in your case it might be one) and then access it.
This solution worked for me (using Selenium 3.4.0):
Object[] handles = driver.getWindowHandles().toArray();
String windowHandle = handles[0]+"";
driver.switchTo().window(windowHandle);
After switching to your browser window you should be able to continue your test.
It relys on correctness of your webdriver- if you opened an Edge window the driver you use should be an EdgeDriver.

Capture user input and actions with Selenium WebDriver using Java

Is it possible to capture user input/actions with Selenium WebDriver, in the same way that you can use the Selenium IDE for recording / creating tests?
i.e. when the user enters a URL, clicks a link, fills in a text box, clicks a button etc etc.
I'd like to be able to capture these actions using the WebDriver rather than just using the Selenium IDE, as I want to integrate with other classes available in my Java application.
I attempted to offer a viable solution in Record Actions using Selenium
Hope this helps.
You can't 'record' a set of actions with Selenium WebDriver, you will need to write those steps manually.
Strictly speaking you can capture user input by using the WebDriver API in your chosen language (C#, Java, PHP, Ruby. Python, Perl or JavaScript) and it vaguely resembles using the DOM. If it suits your requirements you could use configuration files to supply some of your user input.
Navigate to a URL:
WebDriver driver = new FirefoxDriver();
driver.get('url')
Click a link/button:
WebElement element = driver.findElement(By.id("coolestWidgetEvah"));
element.click();
Enter Text in a field:
WebElement element = driver.findElement(By.id("coolestWidgetEvah"));
element.sendKeys('userinput');
For more information on the API Selenium HQ is pretty definitive:
http://seleniumhq.org/docs/03_webdriver.html#introducing-the-selenium-webdriver-api-by-example
If you're going from Selenium IDE to writing tests it'd be really useful to check out the page object pattern as I've found it makes your tests more maintainable in the long-run. This link is a good starting point because it gives an overview, and a visual representation of what you get by following the pattern:
http://blog.josephwilk.net/cucumber/page-object-pattern.html
Hope that helps.
As far as I'm aware, there isn't an easy way to do it - but recording on IDE and exporting as a java file has worked well for me (File -> Export test case as...). I usually do it to c# but have used it with java.

Categories