我想在使用selenium进行GUI测试时使用browsermob来监控网络连接。我找到了一些信息和文档here和here以及here,但它绝对不清楚如何真正使用它。
在文档中写着:
server = Server("path/to/browsermob-proxy")
但那条道路是什么?在哪里找到它?
我也看到
java -jar browsermob.jar --port 9090
但是根本没有解释这个jar文件是什么,如果它是browsermob安装的一部分,或者是不相关的东西。
如果有人可以提供有关如何使用浏览器的完整和工作示例以及我需要安装的所有内容,我将不胜感激...
答案 0 :(得分:11)
BrowserMob Proxy
强> BrowserMob Proxy
是一种开源工具,用于捕获HAR格式的Web应用程序的性能数据。它还允许操纵浏览器行为和流量,例如模拟网络流量,重写HTTP请求和响应等,以及操纵来自其AJAX应用程序的网络流量。简而言之,BrowserMob proxy
帮助我们使用Selenium WebDriver自动化测试捕获Web应用程序的客户端性能数据。
您可以从Python Documentation和this tutorial找到有关 BrowserMob Proxy
的详细信息。
通过CLI安装 browsermob-proxy :
C:\Users\your_user>pip install browsermob-proxy
Collecting browsermob-proxy
Downloading browsermob-proxy-0.8.0.tar.gz
Collecting requests>=2.9.1 (from browsermob-proxy)
Downloading requests-2.18.4-py2.py3-none-any.whl (88kB)
100% |¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦| 92kB 148kB/s
Collecting idna<2.7,>=2.5 (from requests>=2.9.1->browsermob-proxy)
Downloading idna-2.6-py2.py3-none-any.whl (56kB)
100% |¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦| 61kB 639kB/s
Collecting urllib3<1.23,>=1.21.1 (from requests>=2.9.1->browsermob-proxy)
Downloading urllib3-1.22-py2.py3-none-any.whl (132kB)
100% |¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦| 133kB 250kB/s
Collecting certifi>=2017.4.17 (from requests>=2.9.1->browsermob-proxy)
Downloading certifi-2017.11.5-py2.py3-none-any.whl (330kB)
100% |¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦| 337kB 401kB/s
Collecting chardet<3.1.0,>=3.0.2 (from requests>=2.9.1->browsermob-proxy)
Downloading chardet-3.0.4-py2.py3-none-any.whl (133kB)
100% |¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦| 143kB 296kB/s
Installing collected packages: idna, urllib3, certifi, chardet, requests, browse
rmob-proxy
Running setup.py install for browsermob-proxy ... done
Successfully installed browsermob-proxy-0.8.0 certifi-2017.11.5 chardet-3.0.4 id
na-2.6 requests-2.18.4 urllib3-1.22
Download
browsermob-proxy 二进制文件 browsermob-proxy-2.1.4-bin 形成以下网址:
https://bmp.lightbody.net/
Extract
和Save
C:\Utility
Launch
Browsermobproxy服务器:
C:\Utility\browsermob-proxy-2.1.4\lib>java -jar browsermob-dist-2.1.4.jar --port 9090
Running BrowserMob Proxy using LittleProxy implementation. To revert to the legacy implementation, run the proxy with the command-line option '--use-littleproxy false'.
[INFO 2018-01-17T19:01:30,276 net.lightbody.bmp.proxy.Main] (main) Starting BrowserMob Proxy version 2.1.4
[INFO 2018-01-17T19:01:30,388 org.eclipse.jetty.util.log] (main) jetty-7.x.y-SNAPSHOT
[INFO 2018-01-17T19:01:30,471 org.eclipse.jetty.util.log] (main) started o.e.j.s.ServletContextHandler{/,null}
[INFO 2018-01-17T19:01:30,871 org.eclipse.jetty.util.log] (main) Started SelectChannelConnector@0.0.0.0:9090
您也可以通过以下代码启动 Browsermobproxy服务器。
创建一个新的PyDev
模块(如果使用Eclipse
)并通过IDE
编写基本程序,如下所示:
from browsermobproxy import Server
server = Server("C:\\Utility\\browsermob-proxy-2.1.4\\bin\\browsermob-proxy")
server.start()
proxy = server.create_proxy()
from selenium import webdriver
profile = webdriver.FirefoxProfile()
profile.set_proxy(proxy.selenium_proxy())
driver = webdriver.Firefox(firefox_profile=profile)
proxy.new_har("google")
driver.get("http://www.google.co.in")
proxy.har # returns a HAR JSON blob
server.stop()
driver.quit()
快照:
Test
Python Run Program
执行时,您会看到 Firefox Quantum Browser 被初始化,url
http://www.google.co.in 会打开并关闭在测试结束时。完成Test Execution
后,您会在工作空间中找到以下文件,这些文件将为您提供Test Execution
的所有详细信息:
bmp.log
geckodriver.log
server.log
答案 1 :(得分:4)
from browsermobproxy import Server
import psutil
import time
for proc in psutil.process_iter():
# check whether the process name matches
if proc.name() == "browsermob-proxy":
proc.kill()
dict = {'port': 8090}
server = Server(path="./BrowserMobProxy/bin/browsermob-proxy", options=dict)
server.start()
time.sleep(1)
proxy = server.create_proxy()
time.sleep(1)
from selenium import webdriver
profile = webdriver.FirefoxProfile()
selenium_proxy = proxy.selenium_proxy()
profile.set_proxy(selenium_proxy)
driver = webdriver.Firefox(firefox_profile=profile)
proxy.new_har("google")
driver.get("http://www.google.co.uk")
print (proxy.har) # returns a HAR JSON blob
server.stop()
driver.quit()
有两件事,如果你的代码失败了,那么这个过程有时会被打开。所以我在下面添加了相同的
import psutil
import time
for proc in psutil.process_iter():
# check whether the process name matches
if proc.name() == "browsermob-proxy":
proc.kill()
创建代理之前和之后1秒的睡眠
server.start()
time.sleep(1)
proxy = server.create_proxy()
time.sleep(1)
这有助于消除服务器花费时间开始可能面临的一些间歇性问题
答案 2 :(得分:2)
您需要配置驱动程序以将BMP用作代理,以便它可以记录网络活动。这是一个例子......
package com.howaboutthis.satyaraj.videntify;
import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.google.firebase.auth.FirebaseAuth;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.text.SimpleDateFormat;
import java.util.Date;
public class IdentifyActivity extends AppCompatActivity implements View.OnClickListener {
private static final int PICK_IMAGE_REQUEST = 1;
private static final int REQUEST_TAKE_PHOTO = 200;
private static final int CLICK_IMAGE_REQUEST = 2;
private static final String TAG = "IdentifyActivity";
private RequestPermissionHandler mRequestPermissionHandler;
String mCurrentPhotoPath;
String userName;
String userEmail;
Uri profileURL;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_identify);
mRequestPermissionHandler = new RequestPermissionHandler();
Bundle bundle = getIntent().getExtras();
assert bundle != null;
userName = bundle.getString("username");
Button cameraButton = findViewById(R.id.camera);
Button galleryButton = findViewById(R.id.gallery);
cameraButton.setOnClickListener(this);
galleryButton.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_profile) {
Intent intent = new Intent(IdentifyActivity.this, ProfileActivity.class);
intent.putExtra("username", userName);
intent.putExtra("user_email", userEmail);
intent.putExtra("profile_url",profileURL);
startActivity(intent);
}
else if (id == R.id.action_about)
return true;
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.camera:
takePicture();
break;
case R.id.gallery:
pickFromGallery();
break;
}
}
private void pickFromGallery() {
String[] PERMISSIONS = {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA};
mRequestPermissionHandler.requestPermission(this, PERMISSIONS, REQUEST_TAKE_PHOTO, new RequestPermissionHandler.RequestPermissionListener() {
@Override
public void onSuccess() {
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(intent, PICK_IMAGE_REQUEST);
}
@Override
public void onFailed() {
Toast.makeText(IdentifyActivity.this, "Request permission failed", Toast.LENGTH_SHORT).show();
}
});
}
private void takePicture() {
String[] PERMISSIONS = {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE};
mRequestPermissionHandler.requestPermission(this, PERMISSIONS, REQUEST_TAKE_PHOTO, new RequestPermissionHandler.RequestPermissionListener() {
@Override
public void onSuccess() {
dispatchTakePictureIntent();
}
@Override
public void onFailed() {
Toast.makeText(IdentifyActivity.this, "Request permission failed", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onBackPressed() {
super.onBackPressed();
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
System.exit(0);
}
@Override
protected void onDestroy() {
super.onDestroy();
finish();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri uri = data.getData();
File finalFile = new File(getRealPathFromURI(uri));
Intent intent = new Intent(IdentifyActivity.this, detailedActivity.class);
intent.putExtra("PATH", finalFile.getPath());
startActivity(intent);
} else if (requestCode == CLICK_IMAGE_REQUEST && resultCode == RESULT_OK) {
galleryAddPic();
Intent intent = new Intent(IdentifyActivity.this, detailedActivity.class);
intent.putExtra("PATH", mCurrentPhotoPath);
startActivity(intent);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
mRequestPermissionHandler.onRequestPermissionsResult(requestCode, permissions,
grantResults);
}
public String getRealPathFromURI(Uri uri) {
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(uri, filePathColumn, null, null, null);
String picturePath = null;
if (cursor != null) {
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
picturePath = cursor.getString(columnIndex);
cursor.close();
}
return picturePath;
}
//Creating a path for the captured image to be saved. Using this path we can retrieve the original Image.
private File createImageFile() throws IOException {
// Create an image file name
@SuppressLint("SimpleDateFormat") String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
Log.e(TAG,"Error while creating the file");
// Error occurred while creating the File
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
"com.howaboutthis.satyaraj.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, CLICK_IMAGE_REQUEST);
}
}
}
// Adding the pic to the gallery so that it is visible to everyone
private void galleryAddPic() {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(mCurrentPhotoPath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
}
}
然后所有活动都将记录到该文件中。
在我的例子中,二进制文件的路径在Windows上使用Python 2.7 <{1}}
答案 3 :(得分:0)
此问题与以下事实有关:Browsermob-proxy的安装不只是在做
pip install browsermob-proxy
执行上述代码后,需要转到https://bmp.lightbody.net,下载zip,解压缩,然后在python脚本中调用Server()时,从刚刚创建的zip中确定可执行文件的路径。它位于“ bin”文件夹中,被称为browsermob-proxy。
在我的情况下是:
server = Server("/anaconda3/lib/python3.7/site-packages/browsermobproxy/browsermob-proxy-2.1.4/bin/browsermob-proxy")