How to get details of on which localhost karate UI test are running - karate

I am running a UI test in karate where I need to fetch the details of local host where test are running in karate, earlier I was using selenium to the same task with the following code
ChromeOptions chromeOptions = new ChromeOptions();
WebDriverManager.chromedriver().setup();
ChromeDriver driver = new ChromeDriver(chromeOptions);
HttpCommandExecutor executor = (HttpCommandExecutor) driver.getCommandExecutor();
URL url = executor.getAddressOfRemoteServer();
And this url was returning
http://localhost:7150
Is there way to get these details in karate framework for UI tests.

I am able to fetch the port number from karate web driver
* def opt = driver.getOptions()
* def port = opt.port
And then appending to string "http://localhost:"+port which solves the problem.

Related

The proxy moz-proxy://host:port is requesting a username and password. The site says: “LDAP”:

I have Integrated the Selenium with JMeter to perform Performance Test, For this I have used Firefox driver Cofig and gecko driver. Script is executing successfully in local window machine But when trying to execute it on Linux server it giving the following error.
"The proxy moz-proxy://host:port is requesting a username and password. The site says: “LDAP”:"
Note:
gecko driver is compatible with Firefox version.
username and password passed while setting proxy on server, and also through JMeter command.
It looks like you're behind a corporate proxy server which requires authentication and unfortunately this is not something you can bypass using Firefox Driver Config
You will need to switch to JSR223 Sampler with Groovy language and instantiate the browser manually providing the proxy host/port/username/password/etc. Take a look at Proxy class JavaDoc
Example code:
import org.openqa.selenium.Proxy
import org.openqa.selenium.firefox.FirefoxDriver
import org.openqa.selenium.firefox.FirefoxOptions
Proxy proxy = new Proxy()
proxy.setHttpProxy("http://username:password#proxy-host:proxy.port")
proxy.setSslProxy("http://username:password#proxy-host:proxy.port")
FirefoxOptions options = new FirefoxOptions()
options.setCapability("proxy", proxy)
FirefoxDriver driver = new FirefoxDriver(options)
driver.get('http://example.com')

Selenium WebDriver connection to Kameleo browser

So I have figured out how to get started and open a Kameleo browser profile using Python. However, I find the Session ID and Port the chrome browser was started with. I think I have this, but my session ID is throwing an error.
I was expecting the /profiles/{guid}/start endpoint would return a JSON dictionary with the session id and port, also would be nice to have that under the profiles/{guid}/status http calls. I couldn't find it in the swaggerhub documentation.
This is the code I'm using
from kameleo.local_api_client.kameleo_local_api_client import KameleoLocalApiClient
from kameleo.local_api_client.builder_for_create_profile import BuilderForCreateProfile
client = KameleoLocalApiClient()
base_profiles = client.search_base_profiles(
device_type='desktop',
browser_product='chrome'
)
# Create a new profile with recommended settings
# for browser fingerprinting protection
create_profile_request = BuilderForCreateProfile \
.for_base_profile(base_profiles[0].id) \
.set_recommended_defaults() \
.build()
profile = client.create_profile(body=create_profile_request)
# Start the browser
client.start_profile(profile.id)
According to the documentation you don't need to get the port and the sessionID manually as you can make the connection to the browser through Kameleo.CLI.exe port.
If you keep on reading the README you will find an example where they showcase the W3C WebDriver connection.
# Connect to the running browser instance using WebDriver
options = webdriver.ChromeOptions()
options.add_experimental_option("kameleo:profileId", profile.id)
driver = webdriver.Remote(
command_executor=f'{kameleoBaseUrl}/webdriver',
options=options
)
# Use any WebDriver command to drive the browser
# and enjoy full protection from Selenium detection methods
driver.get('https://google.com')
I could also find this code in Kameleo's example repository.

How to interact with Developer tools in chrome using selenium or any other library

I am trying to read or interact with network and console tabs of developer tools in chrome. Could you please guide me how to achieve this.
Thanks
The short answer is no.
How to open Chrome Developer console in Selenium WebDriver using JAVA. As the provided link states you cannot directly access the chrome developer tools.
But if you are interested in access the contents of the browser console and network tab, selenium provides you a way.
System.setProperty("webdriver.chrome.driver", getChromeDriverLocation());
LoggingPreferences loggingprefs = new LoggingPreferences();
loggingprefs.enable(LogType.BROWSER, Level.WARNING);
loggingprefs.enable(LogType.PERFORMANCE, Level.WARNING);
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(CapabilityType.LOGGING_PREFS, loggingprefs);
driver = new ChromeDriver(capabilities);
You can then print the logs as needed
LogEntries logEntries = SeleniumBaseTest.getWebDriver().manage().logs()
.get(org.openqa.selenium.logging.LogType.BROWSER);
for (LogEntry entry : logEntries) {
System.out.println((String.format("%s %s %s\n", new Date(entry.getTimestamp()), entry.getLevel(),
entry.getMessage())));
}
LogType.BROWSER will give you the browser console.
Logtype.PERFROMANCE will give you the network tab.
Other ways to access the network tab is to use a browser proxy to record the transactions.
http://www.seleniumeasy.com/selenium-tutorials/browsermob-proxy-selenium-example
In Python, Pychrome is working fine as an interface to DevTools Protocol Viewer.
Below is an example I used with a mix of Selenium for the main request and Pychrome as I wanted to get Images without downloading them twice...
import base64
import pychrome
def save_image(file_content, file_name):
try:
file_content=base64.b64decode(file_content)
with open("C:\\Crawler\\temp\\" + file_name,"wb") as f:
f.write(file_content)
except Exception as e:
print(str(e))
def response_received(requestId, loaderId, timestamp, type, response, frameId):
if type == 'Image':
url = response.get('url')
print(f"Image loaded: {url}")
response_body = tab.Network.getResponseBody(requestId=requestId)
file_name = url.split('/')[-1].split('?')[0]
if file_name:
save_image(response_body['body'], file_name)
tab.Network.responseReceived = response_received
# start the tab
tab.start()
# call method
tab.Network.enable()
# get request to target the site selenium
driver.get("https://www.realtor.com/ads/forsale/TMAI112283AAAA")
# wait for loading
tab.wait(50)

cannot get to Selenium Proxy when setting up through BrowserMob

I am able to create a Selenium proxy using BrowserMob everything work well on my my local PC. When I run the same code on a server (Windows Server 2008 R2 Standard) it errors our "cannot connect to tunnel".
I have tried different combinations of Chrome swithes like --ignore-certificate-errors,--user-data-dir=C:/temp/insecurechrome,--ignore-certificate-errors. I have ensured that .setTrustAllServer(true) is set. I have tried adjusting the Windows Firewal without any effect.
I will add my code I am using, however, it does work on my local PC, yet not on the server. I am hoping someone can suggest other setting on the server I can changer or something in my code I may have missed.
I first get a Chrome Browser message: wating for Proxy Tunnel. Several seconds later (15-20). I get the error: ERR_TUNNEL_CONNECTION_FAILED.
browserMobProxyServer = new BrowserMobProxyServer();
browserMobProxyServer.setTrustAllServers(true);
browserMobProxyServer.start(0);
port = browserMobProxyServer.getPort();
seleniumProxy = ClientUtil.createSeleniumProxy(browserMobProxyServer);
ChromeOptions options = new ChromeOptions();
options.addArguments("--proxy-server","--ignore-certificate-errors","--user-data-dir=C:/temp/insecurechrome");
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("credentials_enable_service", false);
prefs.put("profile.password_manager_enabled", false);
options.setExperimentalOption("prefs", prefs);
PropertyConfigurator.configure("./resources/properties/log4j.properties");
DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
desiredCapabilities.setCapability(ChromeOptions.CAPABILITY, options);
desiredCapabilities.setCapability(CapabilityType.PROXY, seleniumProxy);
//desiredCapabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true); //Has no effect
driverService = new ChromeDriverService.Builder().usingDriverExecutable(new File("./resources/driver/chromedriver.exe")).usingPort(Integer.parseInt(portRequested)).build();
driverService.start();
return new ChromeDriver((ChromeDriverService)driverService, desiredCapabilities);
I was able to figure out my own issue. There was an existing corporate proxy that was intercepting the traffic. This proxy has different protocols for servers and for users. When running on my PC it ran fine. When running my program on a server I needed to address proxy forwarding or chaining. I accomplished this by adding the below lines to my above code:
import java.net.InetSocketAddress;
...
...
InetSocketAddress x = new InetSocketAddress("proxy.example.com", 80);
browserMobProxyServer.setChainedProxy(x);

Running Selenium scripts on Bluemix

I am completely new to testing and Bluemix. As per my requirement, I need to run Selenium scripts on Bluemix. I am using Automated Accessibility Tester service in Bluemix for running Selenium scripts. I have added the service to my space and added the aat.jar and the Tool Configuration JSON file to my working directory. I have added some scripts to my test case for running on remote server. However, I am getting the following error message:
error Message: setEnabledIds INFO: Enabled Rulesets: IBM_DCP080115
FAILED CONFIGURATION: #BeforeClass setUp
org.openqa.selenium.remote.UnreachableBrowserException: Could not
start a new session. Possible causes are invalid address of the remote
server or browser start-up failure.
This is the script that I have added:
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
ChromeOptions options = new ChromeOptions();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
ComplianceConfig config = new ComplianceConfig(new String[] { "IBM_DCP080115" });
driver = ComplianceDriverRemote.init(config, capabilities);
I am getting an error at driver = ComplianceDriverRemote.init(config, capabilities);
For running test cases on Bluemix, I am following this link: https://console.ng.bluemix.net/docs/services/ecsdashboard/index.html