Is there any selenium/python script for the Screen Recording? - selenium

I want to record the screen by using selenium in python, I searched for these, but I only get results for java, and for the screenshots. Please let me know if there is any script by which I can record the screen.

you need record the whone screenshot? try this library
and try this code:
from clicknium import clicknium as cc, locator, ui
cc.get_screenshot("D:\\test.jpg")

Try this, very popular framework for eg. java or python etc.
in cmd line : pip install allure-pytest
go here: https://docs.qameta.io/allure/
section 2.1. Installing a commandline
download the newest version for eg. windows xxx.zip
copy path of bin folder eg: D:\Drivers\allure-2.18.1\bin
and paste it to Environment Variables > find 'Path' and edit it> add new path with bin path.
to Your test.py file:
import allure
from allure_commons.types import AttachmentType
and add it at the end of Your test in eg.: assertion statement
example:
method_name = self.driver.find_element(By.XPATH, "xxx").text
if method_name == 'some text':
assert True
else:
allure.attach(self.driver.get_screenshot_as_png(), name="test_name",
attachment_type=AttachmentType.PNG)
assert False
In IDE terminal or cmd do this:
pytest -v -s --alluredir="D:\projectPath\raport" Path/to/your/tests
then tests will be executed
in terminal do this to read raport wfrom test:
allure serve "D:\raport\Path"
with this allure framework You will have reports of tests with screenshots:

Related

Webdriver Issue

I trying to use selenium in jupyter notebook on Firefox. I have installed the geckodriver in /usr/bin directory .When I run my code it says:
WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
if I run the command %env PATH following output is displayed:
$PATH:/usr/bin/
from selenium import webdriver
browser = webdriver.Firefox()
I have even tried
browser=webdriver.Firefox(executable_path=r'/usr/bin/geckodriver')
Can somebody help me solve this?
I hope you are working with geckodriver on the local machine and providing the absolute path for the driver to run, This can be done automatically using webdriver-manager python
Advantages of using it are-
You don't have to give the path every time to run code.
Makes the code portable and easy to run on any machine without checking for geckodriver or chromedriver.
pip install webdriver-manager
Now the above code in the question can be changed to work simply with,
from selenium import webdriver
from webdriver_manager.firefox import GeckoDriverManager
driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())
If the above doesn't help, try doing this.
autoinstaller for geckodriver-
pip install geckodriver-autoinstaller
from selenium import webdriver
import geckodriver_autoinstaller
geckodriver_autoinstaller.install() #checks for driver or install it. driver =
webdriver.Firefox() driver.get("python.org")
this worked fine for me, otherwise, try to give a full path after downloading the latest geckodriver binary file.
Try to implement this with a Maven project so you can add dependencies in the pom.xml file. so you dont have to give the path each time. Refer this article here.Its in java but will help.

nw.js chromedriver won't run from outside of the project folder

I am trying to set up automated tests on an nw.js based app using selenium-python with chromedriver and for practical reasons (frequent reinstallation...) I want to keep chromedriver separated from the rest of the files in another folder. My tests work only when the chromedriver is located at the same folder as the rest of the project (along with nw.exe). If I try to place it anywhere else and alter paths with 'binary_location', 'chrome_driver_binary' and 'add_argument' accordingly, I always end up with exceptions such as
selenium.common.exceptions.WebDriverException: Message: unknown error:
cannot find Chrome binary
or
selenium.common.exceptions.WebDriverException: Message: unknown error:
Failed to create a Chrome process
Nw.js documentation wasn't helpful as it only says the following:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("nwapp=/path/to/your/app")
driver = webdriver.Chrome(executable_path='/path/to/nwjs/chromedriver', chrome_options=chrome_options)
Thanks in advance for any ideas.
I was able to do this but I used ruby. It should be possible to translate to python.
Also I'm running Ubuntu 20.04. No idea if this will all work in Windows.
chrome_options.add_argument("nwapp=/path/to/your/app")
This line doesn't specify, but the nwapp should be a directory.
If you build your app using phoenix builder, then you probably have a line like this in package.json:
"package:linux": "npm run build:prod && build --tasks linux-x64 --mirror https://dl.nwjs.io/ .",
For me, this command creates a directory called packages/myapp-0.0.1-linux-x64
Inside of there you should see an executable "myapp" and the manifest "package.json"
The nwapp= line should point to this directory. It will use the manifest file to get the app name. In ruby that looks like this:
myapp_dir = File.join(root_dir, 'packages', 'myapp-0.0.1-linux-x64')
chrome_options.add_argument("nwapp=" + myapp_dir)
executable_path='/path/to/nwjs/chromedriver'
When you install nw.js with npm, it will create a directory tree: node_modules/nw/nwjs Inside that directory is the chromedriver executable.
The "executable_path" is the full path to chromedriver executable. In ruby, it looks like this:
chromedriver_path = File.join(root_dir, 'node_modules', 'nw', 'nwjs', 'chromedriver')
Selenium::WebDriver::Chrome::Service.driver_path = chromedriver_path
#driver = Selenium::WebDriver.for :chrome, options: chrome_options
Ruby does everything relative to where you invoke it, so I added a script in the root of my nw.js app and then added this line to package.json:
"e2e": "ruby ./e2e.rb"
Then this runs via npm run e2e

How to run groovy based JUnit Test Suite from the command line?

How to run the JUnit test suite containing a set of test cases(groovy based) from the command line. Following is the test suite class generated by eclipse.
package com.example.testclasses;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
#RunWith(Suite.class)
#SuiteClasses({ abc.class, xyz.class })
public class AllTests {
}
The above test suite works when I run the above test suite(AllTests)as JUnit from eclipse, however, I want to run the test suite(AllTests) from the command line. How do I do this?
Info: I am using Geb(Groovy) based testing where all the test cases(example: abc, def) are groovy based(having .groovy extension).
If you wish to run your tests from the command line I would suggest using a build system. My personal choice would be to use Gradle but you could probably also get away with using Maven.
The benefit of using a build system, apart from being able to run the tests from the command line, is that it will help you manage your dependencies and it will be easier to build the project for others working on the same codebase - they won't have to manually setup all the dependencies and their versions in the IDE.
Try this:
java -cp /path/to/groovy/embeddable/groovy-all-1.8.1.jar groovy.lang.GroovyShell AllTests.groovy
where 1.8.1 should be replaced with your version of groovy-all-*.jar

Run Groovy Scripts in IntelliJ fails

I can't run or debug Groovy scripts in IntelliJ. I'm getting the error: Error: Could not find or load main class org.codehaus.groovy.tools.GroovyStarter. Running scripts from cmd works propertly (I'm using groovyc and groovy command).
import org.apache.pdfbox.pdmodel.PDDocument
import org.fit.pdfdom.PDFDomTree
import org.w3c.dom.Document
// load the PDF file using PDFBox
PDDocument pdf = PDDocument.load(new java.io.File("file.pdf"))
// create the DOM parser
PDFDomTree parser = new PDFDomTree()
// parse the file and get the DOM Document
Document dom = parser.createDOM(pdf)
I missed https://mvnrepository.com/artifact/org.codehaus.groovy/groovy-all/2.4.12 depedency in my pom.xml file. Bug is resolved. Thanks All for your hints :)
Create Run Configuration (⌘N), and set the Module to the Module that contains all of your dependencies.
I had had the same problem until I did the next:
deleted the groovy from "Global Libraries" (Don't forget to click the 'Apply' button
screenshot how to delete groovy
Opened any groovy file, and click on "Configure Groovy SDK"
screenshot how to select groovy
Selected the folder with groovy installation (see on the screenshot above)
After all, I can run/debug my scripts from IDEA
Try to create you script not as Groovy class.
Use this way:
Ctrl + Shift + A -> Groovy Script -> Set script name

Program using selenium fails after building with cx_freeze

I'm developing an automatic web tester using Selenium (v2.37.2). Program works properly until I run the test built with cxfreeze (there is also tkinter gui).
there is the init function
def initDriver(self):
if self.browser == FIREFOX:
profile = webdriver.FirefoxProfile(profile_directory=self.profile);
self._driver = webdriver.Firefox(firefox_profile=profile)
elif self.browser == CHROME:
self._driver = webdriver.Chrome(self.executable, chrome_options=profile)
elif self.browser == IEXPLORER:
self._driver = webdriver.Ie(self.executable)
Now when I build it using Cx_freeze I get this error
method redirectToBlank(...) calls initDriver(..) as the first thingSo how I pack the .xpi file to the library.zip file - which option in setup.py I have to use? And do I even have to this?
And the second strange thing is, that the other browsers work fine, when I execute the .exe file in by clicking on its icon, but when I run it from command line, I get errors even for chrome and IE. (Sorry that the traceback isn't complete)
All paths are relative from the executed file (no matter from where you run it),
Thank you for any ideas to solve this problem.
(method redirectToBlank(...) calls initDriver(..) as the first thing)
First issue solved
It's problem with selenium - FirefoxProfile - class, which tries to load webdriver.xpi as a normal file, but selenium pack all libraries to a zip file, so selenium can't find it.
Even forcing cx_freeze in setup file to add webdriver.xpi to a proper directory in zip won't help.
It is necessary to edit FirefoxProfile (in firefox_profile module) class for example like this
def _install_extension(self, addon, unpack=True):
"""
Installs addon from a filepath, url
or directory of addons in the profile.
- path: url, path to .xpi, or directory of addons
- unpack: whether to unpack unless specified otherwise in the install.rdf
"""
if addon == WEBDRIVER_EXT:
# altered lines
import sdi.env
WEBDRIVER_SUBSTITUTE = "path/to/unpacked/webdrive.xpi"
addon = os.path.join(os.path.dirname(__file__), WEBDRIVER_SUBSTITUTE)
# Original lines:
# addon = os.path.join(os.path.dirname(__file__), WEBDRIVER_EXT)
< the rest of the method >
Issue 2
OSError: win error 6: the handle is invalid problem wasn't caused by either cxfreeze or selenium. I run the final exe file from git bash. There's the problem. For some reason git bash doesn't open stdin for the program and that's why it fails. When I run it in standard windows command line, everything is ok or if i run it from git bash like program.exe < empty_file
what i did was remove selenium form packages list.
and put it inside includefiles, then it works.
like this :
includefiles = [(seleniumPackage,'')]
...
options = {'build_exe': {'includes':includes,
'excludes':excludes,
'optimize':2,
'packages':packages,
'include_files':includefiles,
...