Linux 如何让 Karma 与 Chrome 一起使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19671999/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 01:13:53  来源:igfitidea点击:

How to get Karma to work with Chrome

linuxgoogle-chromeangularjsubuntukarma-runner

提问by Alice

I have a command-line-only Ubuntu 11.04 (GNU/Linux 2.6.35.4-rscloud x86_64) and I'm working through the Angular Phonecat tutorialwhich uses the Karma for testing. The Karma browser config pagesays Chrome launcher is "shipped with Karma by default."

我有一个仅限命令行的 Ubuntu 11.04 (GNU/Linux 2.6.35.4-rscloud x86_64),我正在学习使用 Karma 进行测试的Angular Phonecat 教程。该噶浏览器配置页说Chrome启动的是“默认附带噶。”

But it's not working for me. This is the karma.conf.js. Below is the output.

但这对我不起作用。这是 karma.conf.js。下面是输出。

 $ ./scripts/test.sh

Starting Karma Server (http://karma-runner.github.io)
-------------------------------------------------------------------
INFO [karma]: Karma v0.10.4 server started at http://localhost:9876/
INFO [launcher]: Starting browser Chrome
ERROR [launcher]: Cannot start Chrome
        Can not find the binary google-chrome
        Please set env variable CHROME_BIN

This answerpoints out that I need chromium.

这个答案指出我需要铬。

So I tried to install Chrome on my Ubuntu with this guidewith "download 64 bit version using command line". Everything went well until sudo apt-get -f install, which ended with a lot of "failed to fetch" from ubuntu IP addresses, such as 91.189.91.15 or 91.189.92.. Even with /usr/bin/googlesomething* that the "Can not find the binary google-chrome" goes away, I still got the "Cannot start Chrome" on the individual test level. Then while trying to fix errors, I deleted /usr/bin/googlesomething.

因此,我尝试使用本指南在我的 Ubuntu 上安装 Chrome,其中包含“使用命令行下载 64 位版本”。一切都很顺利,直到sudo apt-get -f install,以从 ubuntu IP 地址(例如 91.189.91.15 或 91.189.92)出现很多“无法获取”而告终。. 即使 /usr/bin/google某些东西*“找不到二进制 google-chrome”消失了,我仍然在个人测试级别得到“无法启动 Chrome”。然后在尝试修复错误时,我删除了 /usr/bin/google something

Currently, I have these node_modules:

目前,我有这些 node_modules:

angular-phonecat/node_modules$ ls
karma                      karma-html2js-preprocessor  karma-requirejs
karma-chrome-launcher      karma-jasmine               karma-script-launcher
karma-coffee-preprocessor  karma-junit-reporter
karma-firefox-launcher     karma-phantomjs-launcher

Q 1: Since my machine has only command-line access, no GUI, is the other Karma launcher, PhantomJS, a better choice than Chrome/Chromium?

Q 1:由于我的机器只有命令行访问,没有 GUI,其他 Karma 启动器 PhantomJS 是不是比 Chrome/Chromium 更好的选择?

Q 2: If I should still use Chrome/Chromium, should I get Chrome or Chromium?

问题 2:如果我仍然应该使用 Chrome/Chromium,我应该使用 Chrome 还是 Chromium?

Q 3: Does anyone know what exactly do I need to get google-chrome or chromium for Karma to work in the Angular app?

Q 3:有谁知道我需要什么才能让 Karma 在 Angular 应用程序中工作?

回答by Ludwig Magnusson

  1. If you only have a command-line interface, PhantomJS is the only choice for you.
  2. In linux (at least 12.04 and forwards) chromium is the alternative. But you can't install it if you don't have a graphical ui.
  3. The clue is here: "Please set env variable CHROME_BIN". Karma tries to execute a file called google-chromeand it does not exist. The name of the executable for chrome varies from OS to OS. Therefore you need to set an enviroment variable called CHROME_BIN which has the value of the name of your chrome executable. On my system (Linux desktop 13.10) this is chromium-browser.
  1. 如果你只有命令行界面,PhantomJS 是你唯一的选择。
  2. 在 linux(至少 12.04 及更高版本)中,chrome 是替代方案。但是如果您没有图形用户界面,则无法安装它。
  3. 线索在这里:“请设置环境变量 CHROME_BIN”。Karma 尝试执行一个名为的文件google-chrome,但该文件不存在。chrome 的可执行文件的名称因操作系统而异。因此,您需要设置一个名为 CHROME_BIN 的环境变量,它的值是您的 chrome 可执行文件的名称。在我的系统(Linux 桌面 13.10)上,这是chromium-browser.

回答by Daniel Bidulock

I'll expand on Ludwig's third point for answer seekers and for my own reference in the future...

我将扩展路德维希的第三点,供寻求答案的人使用,并在未来供我自己参考……

The first thing you need to do is find where chromium-browseris installed. Run:

您需要做的第一件事是找到chromium-browser安装位置。跑:

which chromium-browser

This will return the path to the executable, which will look something like this:

这将返回可执行文件的路径,如下所示:

/usr/bin/chromium-browser

Then simply set the path:

然后简单地设置路径:

export CHROME_BIN=/usr/bin/chromium-browser

Now Karma can find the browser it needs to execute your tests (assuming you've decided to use a graphical interface).

现在 Karma 可以找到执行测试所需的浏览器(假设您已决定使用图形界面)。

回答by turtlemonvh

When I set up a project with the yeoman generator angulartoday, I couldn't get chrome or phantomjs to work with karma either.

当我今天使用yeoman 生成器 angular设置项目时,我也无法让 chrome 或 phantomjs 与 karma 一起工作。

It turns out this is an issue specific to that projectand the fix is simply adding the missing plugins to the package.json in your project root.

事实证明这是一个特定于该项目的问题,修复只是将缺少的插件添加到项目根目录中的 package.json 中。

回答by Gh111

My solution is to create an alias in linux to windws's chrome

我的解决方案是在linux中为windws的chrome创建一个别名

alias launchchrome="\"/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe\""

and after that export that alias as linux env variable

然后将该别名导出为 linux env 变量

export CHROME_BIN=launchchrome

That's it, now if you run ng testyour windows chrome will be used! In my case it worked If you will facing some problems with this solution pleas leave some comment I'll edit my answer.

就是这样,现在如果你运行ng test你的 windows chrome 将被使用!就我而言,它有效如果您在使用此解决方案时遇到一些问题,请留下一些评论,我将编辑我的答案。