I got everything working with Ironworker and casperjs and I have one more step which fails: enabling cookie support in this setup.
I'm starting casper with the --cookies-file=cookies.txt param and the cookies.txt file is chmoded with the right permissions but somehow there's no cookie support when I visit a test page and screenshot it.
Any idea how to enable this feature with IronWorker?
Based on casperjs example but without phantomjs (it is already included in phantom-1.9 stack)
casper.worker:
runtime "binary"
stack "phantom-1.9"
exec "run.sh"
# Include the CasperJS library
dir "casperjs"
# Include the Javascript file that Casper will execute
file "simple.js"
run.sh:
casperjs/bin/casperjs --verbose --ignore-ssl-errors=yes --ssl-protocol=any --cookies-file=cookies.txt simple.js
simple.js:
var casper = require('casper').create();
casper.userAgent('Mozilla/5.0 (X11; Linux i586; rv:31.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.99 Safari/537.36')
casper.start('https://hud.iron.io/', function() {
this.fill('form[action="/sessions"]', { email: 'foo#bar.com', password: 'super_hard_pass' }, true);
this.click('input[name="commit"]');
this.echo(this.getTitle());
});
casper.wait('10000');
casper.thenOpen('https://hud.iron.io/account', function() {
this.echo(this.getTitle());
this.echo(this.evaluate(function() {return document.querySelector(".account-header").innerText}));
});
casper.run();
Related
My Playwright UI tests (which use page, not request) work fine. Only the API tests are failing. For example const response = await request.get('https://reqres.in/api/users/3')
I found that it must be the corporate proxy by leaving the corporate wifi, joining my phone's hotspot, and seeing the tests pass. I've played with VSCode proxy settings but have not found one that fixes the timeout. It also fails when I run the test script directly from the root directory with npx playwright test --config=api.config.ts so I don't think it's VSCode.
The error message (containing my code) is below, the problem comes from the get method. The script times out there, and the code after it is never attempted. All API methods, such as get, post delete time out similarly.
1) [chromium] › api.spec.ts:6:10 › API Testing › Simple API Test - Assert Response Status ========
apiRequestContext.get: read ETIMEDOUT
=========================== logs ===========================
→ GET https://reqres.in/api/users/3
user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.5195.19 Safari/537.36
accept: */*
accept-encoding: gzip,deflate,br
============================================================
5 |
6 | test.only('Simple API Test - Assert Response Status', async ({ request }) => {
> 7 | const response = await request.get('https://reqres.in/api/users/3')
| ^
8 | console.log(response)
9 | //expect(response.status()).toBe(200)
10 |
at /Users/USERNAME/playwright-framework/tests/api/api.spec.ts:7:40
Solved. The proxy is set within the Playwright config file, along with the rest of the more common configuration items. If required, username and password can be set here too.
For example:
use: {
headless: false,
viewport: { width: 1280, height: 720 },
actionTimeout: 10000,
screenshot: 'only-on-failure',
ignoreHTTPSErrors: true,
proxy: { server: 'http://yourproxy.com:8080/' },
trace: 'off',
video: 'off'
},
I tried making a get call with axios from my Vue js codebase/ environment to Jenkins API and I'm unable to do so.
I've read every resource that I could but wasn't able to fix this particular problem. I even created a .htaccess file to see if it help but wasn't useful.I ran out of options so I came here for help.
Below are the axios codes that I used within my App.vue file.
axios.get(
*URL to access Jenkins that is currently running on a tomcat server*,
{
headers: {
"jenkins-crumb": "* Some numbers and letters*",
},
auth: {
username: "*obvious username*",
password: "*obvious password*"
},
withCredentials: true,
crossdomain: true
}
)
.then(response => (this.info= response)).catch(error => (console.log(error)));
Console log output:
Access to XMLHttpRequest at 'url' from origin 'http://localhost:8080' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Network output:
General
Request URL: URL
Request Method: OPTIONS
Status Code: 403
Remote Address: localhost:8080
Referrer Policy: no-referrer-when-downgrade
Request Headers
Provisional headers are shown
Access-Control-Request-Headers: authorization,jenkins-crumb
Access-Control-Request-Method: GET
Origin: http://localhost:8080
Referer: http://localhost:8080/
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36
Please help!
I am trying to make a POST request to a PHP script that I have deployed on a Pivotal PCF Instance. I have set the essential headers in the PHP script to accept cross domain requests. However, when I make a POST request from the html code shared below, I see that the GET request is sent instead of POST request. In IE 11 browser and I get SCRIPT7002: XMLHttpRequest: Network Error 0x2ef3, Could not complete the operation due to error 00002ef3 error. Also, when I try to post the data from Chrome browser, I see the below request header
Provisional headers are shown
Access-Control-Request-Headers: content-type
Access-Control-Request-Method: POST
Origin: null
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36
It looks like the server is not accepting the POST requests. I am sharing the below server side PHP script and client side html code for reference. I am not sure whether it has something to do with the scripts, server side configurations or SSL certificate.
I have already research a lot and also tried the solution provided in various posts for the similar question on Stackoverflow but none of it helped.
Approaches tried so far,
Approach 1: Using PHP script
Server side PHP script:
<?php
header("Access-Control-Allow-Origin: *");
header('Access-Control-Allow-Methods: GET, POST, DELETE, PUT, OPTIONS');
header('Access-Control-Allow-Headers: Origin, Content-Type, Accept, Authorization, X-Request-With');
header('Access-Control-Allow-Credentials: true');
header('Method:' . $_SERVER['REQUEST_METHOD']);
$response = array(
'status' => 0,
'status_message' =>'Employee Addition Failed.'
);
if (isset($_POST) && !empty($_POST)) {
if (!empty($_POST['username']) && !empty($_POST['credentials'])) {
$response = array(
'empid' => 123,
'emp_name' =>'Some Name',
'method' => 'POST'
);
}
}
if (isset($_GET) && !empty($_GET)) {
$id = 0;
if (isset($_GET['id']) && !empty($_GET['id'])) {
$id = $_GET['id'];
}
$response = array(
'empid' => 123,
'emp_name' =>'Some Name',
'method' => 'GET',
'id' => $id,
'method' => $_SERVER['REQUEST_METHOD'],
'username' => $_POST['username'],
'enable_post_data_reading' => ini_get('enable_post_data_reading')
);
}
header('Content-Type: application/json');
echo json_encode($response);
?>
Client side html:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#send").click(function(){
$.ajax({
url:"https://ui-php.apps.pcfdev.prod.demo.int/app/?id=1233",
type:"POST",
data:JSON.stringify({username:'Something', credentials: 'credentials'}),
contentType:"application/json; charset=utf-8",
dataType:"json",
success: function(){
console.log('Success');
}
});
});
});
</script>
</head>
<body>
<button id="send">Send</button>
</body>
</html>
When I make a call from Chrome browser, I get the below response.
Response Headers from Chrome browser:
When I make a call from IE11 browser, I see the below header being set automatically by the browser and no response is received.
I see the below error in the console of IE11 browser:
Approach 2:
When I try to do a POST curl request, I see that the code inside the GET block from the PHP script gets executed. Below is the response from curl request.
[Note: I have set proxy in the browser, without it, I will not be able to access the PCF API]
I have the following node route using selenium and chrome driver which is working correctly and returning expected html in the console:
app.get('/google', function (req, res) {
var driver = new webdriver
.Builder()
.forBrowser('chrome')
.build();
driver.get('https://www.google.com')
driver
.manage()
.window()
.setSize(1200, 1024);
driver.wait(webdriver.until.elementLocated({xpath: '//*[#id="lst-ib"]'}));
return driver
.findElement({xpath: '//*[#id="lst-ib"]'})
.sendKeys('stackoverflow' + webdriver.Key.RETURN)
.then((html) => {
return driver
.findElement({xpath: '//*[#id="rso"]/div[1]/div/div/div/div'})
.getAttribute("innerHTML")
})
.then((result) => {
console.log(result)
})
.then(() => {
res
.status(200)
.send('ok')
});
I have also installed the phantom js driver and tested that its working by returning the URL title - it works. When I use the above exact route and replace the chrome with phantomjs I get no results returned. There are no errors - just no print out in my console. The status and result are never sent to the browser so it doesn't appear to be stepping through promise chain.
Any suggestions?
The issue was that there was different html being rendered depending on the user agent. By forcing a user agent I was able to retrieve the results i needed.
Here is the code snippet replaced above to get this working.
.Builder()
// .forBrowser('phantomjs')
.withCapabilities(webdriver.Capabilities.phantomjs()
.set("phantomjs.page.settings.userAgent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36"))
.build();
We are doing some tests to connect to the salesforce.com with Phantomjs 2.0.
It was working pretty good, but in the last couple of weeks salesforce.com kept asking for the verification code every new session.
Is anyone experiencing the same issue?
You can whitelist your IP address in order to stop Salesforce for asking the verification code.
Go to Setup -> Security -> Network Access and add your IP to whitelist.
This should perhaps be a comment, but as I do not have the required points to comment please forgive the "answer". This is a working answer, as I'm not very proficient in casper/phantom I will update on attempted solutions. Hopefully my attempts will help someone else.
Is anyone experiencing the same issue?
A: Yes
Problem: As of Salesforce Spring '16 update when logging into SF from a new browser SF authenticates the user with a verification code (can be tested with a new browser eg.firefox). Oddly the issue doesn't replicate with an incognito session.
Proposed solution: Create a cookies file & login to SF with the casper/phantom scripts. Saving the authentication to the JS cookies file, otherwise every login will require a verification.
Testing steps:
We can see the verification form using casper/phantom.
var fs = require('fs'); //top of script
var html = this.getHTML(); //place this in wait_function() after login()
fs.write('./output.html', html, 'w');
We can now "fill" the verification form with the emailed code
this.click('phSearchInput', function () { //'phSearchInput' found in above extract
this.fill('form', {
'phSearchInput' : '*verification_number_here*'
}, true);
2.1 There seems to be an issue with the final redirect after the verification. I'm not sure how much time I can give this (as it's not a priority) and I'm quite new to JS. I'll leave the code here for reference/testing
/*Altered from http://blog.deadlypenguin.com/blog/2013/07/09/logging-into-salesforce-with-casperjs/ */
var LOGIN_URL, LOGIN_USERNAME, LOGIN_PASSWORD, casp;
casp = require('casper').create({
viewportSize: {
width: 1024,
height: 768
},
verbose: true,
logLevel: 'debug'
});
casp.userAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4');
if (!casp.cli.has('username') && !casp.cli.has('password')) {
casp.echo('Usage: $ casperjs sfdclogin.casper.js --username=USERNAME --password=PASSWORD [--prod]').exit(-1);
}
if (casp.cli.has('prod')) {
LOGIN_URL = 'https://test.salesforce.com//';
} else {
LOGIN_URL = 'https://test.salesforce.com/';
}
LOGIN_USERNAME = casp.cli.get('username');
LOGIN_PASSWORD = casp.cli.get('password');
casp.start(LOGIN_URL, function () {
this.log('Logging in', 'debug');
//login
this.fill('form', {
'username': LOGIN_USERNAME,
'pw': LOGIN_PASSWORD
}, true);
this.log('Logged in', 'debug');
});
casp.wait(5000, function () {
this.log('Varification START', 'debug');
this.fill('form', {
'emc': '*verification_number_here*'
}, true);
this.captureSelector('test.png', 'html');
this.log('Varification FINISHED', 'debug');
this.wait(6000)
}, 12000);
casp.then(function () {
//casp.waitForUrl('https://cs31.salesforce.com/home/home.jsp', function() {
this.log('Are we logged in??');
this.captureSelector('test2.png', 'html');
}, 12000);
casp.run();
Add a cookies file in hopes the verification validation is saved there. (I'll get to this when I figure out step 3).