Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
241 views
in Technique[技术] by (71.8m points)

ruby - Acceptance test with selenium and Keyclock: authentication is not working

Problem:

My task is to implement acceptance tests for an application.

Starting simple, I want to test the following:

Scenario: Authenticated employee see its own profile
   Given Employee is Authenticated
   When Employee navigate to his profile page
   Then Employee should see the page title as "EmployeeProfile"

I am stuck trying to use selenium wed-driver to navigate to an Employee profile URL in the application because it is not recognizing the authentication.

The tests will be implemented using Cucumber and Selenium. The steps in Cucumber are being implemented in Ruby using selenium-cucumber. My application uses Keyclock for identity and access management.

What I doing:

This is the way I am instantiating the web-driver

ENV['HTTP_PROXY'] = ENV['http_proxy'] = '127.0.0.1:8080'
proxy = Selenium::WebDriver::Proxy.new(http: '127.0.0.1:8080')
capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(proxy: proxy)
capabilities['acceptSslCerts'] = true
capabilities['ignore-certificate-errors'] = true
$driver = Selenium::WebDriver.for(:chrome, desired_capabilities: capabilities)

In order to navigate to a specific application URL I am trying to authenticate the user first by calling:

def authenticate()
  connection = Faraday.new
  response = connection.post do |request|
      request.url 'http://localhost:8443/auth/realms/REALM/protocol/openid-connect/token'
      request.headers['Content-Type'] = 'application/x-www-form-urlencoded'
      request.headers['Connection'] = 'keep-alive'
      request.body = "client_id=CLIENT_ID&client_secret=CLIENT_SECRET&grant_type=password&username=USER_NAME&password=PASSWORD"
  end
 end

The response to the POST above succeeds and returns an access token that I am using to rewrite the headers of requests sent for example to navigate to the profile URL. I am rewriting the requests using RITM proxy, which intercept requests and add the header as following:

  def intercept_request()
    interceptor.on_request = proc do |req|
      req.header['Authorization'] = 'Bearer ' + $access_token
      req.header['Connection'] = 'keep-alive'
    end
  end

The RITM.start is called when creating the web-driver instance.

The authentication request succeeds and I can print the response and see the access token. However, when I use $driver.get URL to navigate to an Employee profile URL the authentication form is shown again, instead of navigating to the desired URL.

What I tested so far

I split the problem into parts:

  • The interception of request using the proxy is working, I print requests and responses in the console as they happen in the test.
  • I can use HTTP client API to programmatically access an endpoint end and get an Employee profile. First I use postman as a start point, later I automated it with HTTP client API. In this test the proxy was working too.

What is not working is to use the Web-driver to navigate to my application URL after authentication, which looks like is not getting the authorization for its next requests. Have someone suggestion on what I could try?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...