如何从python无头显示chrome页面

时间:2018-10-08 14:11:46

标签: python google-chrome travis-ci

我想使用无头的Chrome在Travis上运行Transcrypt Python to JavaScript编译器的自动测试套件。

但是我还没有找到如何从Python启动Chrome的方法。 Travis一直尝试使用w3m,然后告诉我它无法使用testresuls加载页面。

有人知道我在做什么错吗?

我的.travis.yml是:

dist: xenial
sudo: yes

addons:
    chrome: stable

language: python
python:
    - '3.7'

install: pip install mypy
script: bash ./transcrypt/development/continuous_integration/run.sh

run.sh文件为:

# Make everything executable and the rest
chmod -R 777 .

# Start headless browser instance, Python's webbrowser.open will now refer to this instance
google-chrome-stable --headless --disable-gpu --remote-debugging-port=9222 http://localhost &

# Enable shipment_test.py to find run_python
export PATH=$PATH:./transcrypt

# Run the shipment test
python ./transcrypt/development/shipment/shipment_test.py

Shipment_test.py是:

...
...
...

transpileCommand = 'transcrypt' if commandArgs.inst else 'run_transcrypt'

shipDir = os.path.dirname (os.path.abspath (__file__)) .replace ('\\', '/')
appRootDir = '/'.join  (shipDir.split ('/')[ : -2])

def getAbsPath (relPath):
    return '{}/{}'.format (appRootDir, relPath)

def test (relSourcePrepath, run, extraSwitches, messagePrename = '', nodeJs = False, build = True):
    # Compute some slugs
    sourcePrepath = getAbsPath (relSourcePrepath)
    sourcePrepathSplit = relSourcePrepath.split ("/")

    relTargetDir = f'{"/".join (sourcePrepathSplit [:-1])}/__target__'
    targetDir = getAbsPath (relTargetDir)

    moduleName = sourcePrepathSplit [-1]
    targetPrepath = f'{targetDir}/{moduleName}'

    relMessagePrepath = f'{relTargetDir}/{messagePrename}'
    messagePrepath = getAbsPath (relMessagePrepath)

    # If there are relevant console messages of the compilation process,
    # like with the static typechecking tests, write them into a file that can be served for a visual check
    if not os.path.exists (targetDir):
        os.makedirs (targetDir) # Transcrypt will make targetDir too late, so it has to happen here
    redirect = f' > {messagePrepath}.out' if messagePrename else ''

    # Transit switches
    transitSwitches = ''
    if commandArgs.dextex:
        transitSwitches += '-de '

    buildSwitch = '-b ' if build else ''

    # Compile with Transcrypt
    os.system (f'{transpileCommand} {buildSwitch}-da -sf -de -m -n {transitSwitches}{extraSwitches}{sourcePrepath}{redirect}')

    # Run back to back in CPython
    if run:
        os.system (f'{transpileCommand} -sf -r {switches}{sourcePrepath}')

    # Apply rollup to obtain monolith, since node doesn't support named imports and exports
    if nodeJs:
        os.system (f'rollup {targetPrepath}.js --o {targetPrepath}.bundle.js --f cjs')

    openNewTab = 2
    if not commandArgs.blind:
        if nodeJs:
            os.system (f'start cmd /k node {targetPrepath}.bundle.js'.format (moduleName))
            time.sleep (5)
            webbrowser.open ('http://localhost:8000', new = openNewTab)
            # webbrowser.open ('http://localhost:8090', new = openNewTab)
        else:
            webbrowser.open (f'http://localhost:8000/{relSourcePrepath}.html', new = openNewTab)
            # webbrowser.open (f'http://localhost:8080/{relSourcePrepath}.html', new = openNewTab)

os.system ('cls' if os.name == 'nt' else 'clear')

# Start a node http server in the Transcryp/transcrypt directory
if not commandArgs.blind:
    os.system (f'start python http-server')
    # os.system (f'start cmd /k http-server {appRootDir} -p8080 -c-1')   # -c-1 means 'Clear cache'

# Allow visual check of command line options
os.system (f'{transpileCommand} -h')

# Perform all tests
for switches in (('', '-f ') if commandArgs.fcall else ('',)):
    test ('development/automated_tests/hello/autotest', True, switches)

    ...
    ...
    ...

Travis日志:

Transcrypt (TM) Python to JavaScript Small Sane Subset Transpiler Version 3.7.7
Copyright (C) Geatec Engineering. License: Apache 2.0
Saving target code in: /home/travis/build/QQuick/Transcrypt/transcrypt/development/automated_tests/hello/__target__/org.transcrypt.__runtime__.js
Saving target code in: /home/travis/build/QQuick/Transcrypt/transcrypt/development/automated_tests/hello/__target__/testlet1.js
Saving target code in: /home/travis/build/QQuick/Transcrypt/transcrypt/development/automated_tests/hello/__target__/testlet0.js
Saving target code in: /home/travis/build/QQuick/Transcrypt/transcrypt/development/automated_tests/hello/__target__/itertools.js
Saving target code in: /home/travis/build/QQuick/Transcrypt/transcrypt/development/automated_tests/hello/__target__/org.transcrypt.autotester.html.js
Saving target code in: /home/travis/build/QQuick/Transcrypt/transcrypt/development/automated_tests/hello/__target__/org.transcrypt.autotester.js
Saving target code in: /home/travis/build/QQuick/Transcrypt/transcrypt/development/automated_tests/hello/__target__/autotest.js
Ready
Transcrypt (TM) Python to JavaScript Small Sane Subset Transpiler Version 3.7.7
Copyright (C) Geatec Engineering. License: Apache 2.0
Generating /home/travis/build/QQuick/Transcrypt/transcrypt/development/automated_tests/hello/autotest.html
Ready
w3m: Can't load http://localhost:8000/development/automated_tests/hello/autotest.html.
w3m: Can't load http://localhost:8000/development/automated_tests/hello/autotest.html
...
...
...

1 个答案:

答案 0 :(得分:0)

以下是技巧:

文件.travis.yml,位于GitHub存储库的根目录中:

class TableViewController : UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 2
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as? CLListTableViewCell ?? CLListTableViewCell(style: .default, reuseIdentifier: "cell")
        return cell
    }

    override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return "Section \(section)"
    }
}

文件run.sh:

dist: xenial
sudo: yes

addons:
    chrome: stable
    apt:
        packages:
            - chromium-chromedriver

before_script:
  - ln -s /usr/lib/chromium-browser/chromedriver ~/bin/chromedriver

language: python
python:
    - '3.7'

script:
    - bash ./transcrypt/development/continuous_integration/run.sh

文件shipping_test.py:

# Install Python modules

pip install mypy
pip install selenium

# Show where we are and what's there

pwd
ls -a -l

# Make everything executable and the rest

chmod -R 777 .

# Enable shipment_test.py to find run_python

export PATH=$PATH:./transcrypt

# Run the shipment test

python ./transcrypt/development/shipment/shipment_test.py --unattended