开发者

Does Selenium support headless browser testing?

开发者 https://www.devze.com 2023-04-08 05:30 出处:网络
I\'m looking at Selenium Server at the moment, and I don\'t seem to notice a driver that supports headless browser testing.

I'm looking at Selenium Server at the moment, and I don't seem to notice a driver that supports headless browser testing.

Unless I'm mistaken, it doesn't support it. If you're on X, you can create a virtual framebuffer to hide the browser window, but that's not really a headless browser.

Can anyone enlighten me? Does Selenium support headless br开发者_JAVA百科owser testing?


you need not use PhantomJS as an alternative to Selenium. Selenium includes a PhantomJS webdriver class, which rides on the GhostDriver platform. Simply install the PhantomJS binary to your machine. in python, you can then use:

from selenium import webdriver
dr = webdriver.PhantomJS() 

and voila.


The WebDriver API has support for HTMLUnit as the browser for your testing. Ruby people have been using Capybara for a while for their headless selenium testing so it is definitely doable.


I know this is a old post. Thought it will help others who are looking for an answer.

You can install a full blown firefox in any linux distribution using XVFB. This makes sure your testing is performed in a real browser. Once you have a headless setup, you can use webdriver of your choice to connect and run testing.


Headless browsers are a bad idea. They get you some testing, but nothing like what a real user will see, and they mask lots of problems that only real browsers encounter. You're infinitely better off using a "headed" browser (i.e., anything but HTMLUnit) on a headless environment (e.g., Windows, or Linux with XVFB).


I notice that you say that using an X framebuffer isn't a true headless solution, however, for most, I think it would be acceptable. In addition to that, this service will help get that going for you if you are interested in that as a solution.


Selenium does support headless browser testing in a way. Docker Selenium is maintained by SeleniumHQ. Those docker containers come with xvfb support with them out of the box. There are tools like PhantomJS that you can hook up with Selenium. However, it's not officially supported by Selenium itself.

Much like what others have described, PhantomJS isn't really recommended. The whole point of Selenium is to automate browsers. But why automate a browser no one uses? I never understood how that was overlooked so often by developers..


Yes. Selenium support headless browser testing and it's more faster as well as convient for big amount of test-cases execution.

ChromeOptions cromeOptions = new ChromeOptions();

//Location of browser binary/.exe file
cromeOptions.setBinary("/usr/bin/google-chrome-stable");

cromeOptions.addArguments("--headless");
cromeOptions.addArguments("--no-sandbox");
cromeOptions.addArguments("--disable-gpu");
cromeOptions.addArguments("--window-size=1920,1080");

WebDriver webDriver = new ChromeDriver(cromeOptions);


Yes ,selenium supports headless browser testing...but i found HTMLUnit failing most times...I was searching for an alternative...PhantomJs was really good.you can definitely give it a try it was very fast when compared to other browsers...It is really good for smoke testing...

http://phantomjs.org/


With ruby and macOS: brew install phantomjs then:

driver = Selenium::WebDriver.for :phantomjs


Yes Selenium supports headless browser testing.Headless browsers are faster than real time browsers.


Install chromeDriver and google-chrome-stable version on the linux server, where the tests will be triggered and add the same binaries in your code.

code snippet:

private static String driverPath = "/usr/bin/chromedriver";
static
{
    System.setProperty("webdriver.chrome.driver", driverPath);
    options = new ChromeOptions();
    options.setBinary("/usr/bin/google-chrome-stable");
    options.addArguments("headless");
    driver = new ChromeDriver(options);
}


Here's a "modern answer" on how to use Selenium with xvfb and Firefox driver in an Ubuntu Linux environment running Django/Python:

# install xvfb and Firefox driver
sudo su
apt-get install -y xvfb firefox
wget https://github.com/mozilla/geckodriver/releases/download/v0.19.1/geckodriver-v0.19.1-linux64.tar.gz
tar -x geckodriver -zf geckodriver-v0.19.1-linux64.tar.gz -O > 
/usr/bin/geckodriver
chmod +x /usr/bin/geckodriver

# install pip modules
pip install selenium
pip install PyVirtualDisplay

You can then follow the Django LiveServerTestCase instructions.

To use the driver you just installed, do something like this:

from pyvirtualdisplay import Display
from selenium.webdriver.firefox.webdriver import WebDriver

driver = WebDriver(executable_path='/usr/bin/geckodriver')
display = Display(visible=0, size=(800, 600)).start()

# add your testing classes here...

driver.quit()
display.stop()
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号