I'm trying to carry out a simple acceptance test for learning purpose.
It's a simple authentication scenario : user enters /admin, If not logged it, He gets redirected to /login to fill the form.
When i run the test i get this error :
1) Couldn't login with a password protected area in LoginCest.loginUserWithProperCredentials
Guy couldn't fill field "username","rafael": Field matching id|name|label|value or css or xpath selector does not exist
Scenario Steps:
5. I fill field "username","rafael" <==== RED
4. I see current url equals "/login"
3. I am on page "/admin"
2. So that I Perform administrative tasks
1. As a Site Owner
Now here's my view :
//create.blade.php
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Login</title>
</head>
<body>
<h1>Login</h1>
{{ Form::open() }}
<div>
{{ Form::Label('username', 'Username') }}
{{ Form::Text('username', '') }}
</div>
<div>
{{ Form::label('password', 'Password') }}
{{ Form::password('password', '') }}
</div>
<div>
{{ Form::submit('Login') }}
</div>
{{ Form::close() }}
</body>
</html>
And here's the test :
class LoginCest
{
public function loginUserWithProperCredentials(WebGuy $I){
$I->am("Site Owner");
$I->wantTo("Login with a password protected area");
$I->lookForwardTo("Perform administrative tasks");
$I->amOnPage('/admin');
$I->seeCurrentUrlEquals('/login');
$I->fillField('username', 'rafael');
$I->fillField("password", "123456");
$I->click("Login");
$I->seeCurrentUrlEquals("/admin");
$I->see("Admin area", "h1");
}
}
Any help would be appreciated.
You may copy XPath of username from html, and write:
$I->fillField('//*[#id="addPosDialog"]/div/button','Username');
'//*[#id="addPosDialog"]/div/button' - paste your xpath there.
You may use xpath, name, id and other locators.
When I have problems with fillField i am usually doing something like this.
HTML code in this case is more important than view code.
If my advice will not solve the problem, need to see HTML code.
I had the issue where the html syntax was not correct. Then the phpbrowser works not reliable. Even so it does not look like it in your example.
Related
I currently have this in my view to output data from an array that comes from my controller:
<!DOCTYPE html>
<html>
#foreach ($details as $detail)
<head>
<meta charset="utf-8">
<title>{{ $detail->name }}</title>
</head>
<body>
<h1>in view</h1>
{{ $detail->name }}
<br>
{{ $detail->street }}
<br>
{{ $detail->city }}, {{ $detail->state }}. {{ $detail->zip }}
</body>
#endforeach
</html>
This is the function in my controller:
$details = DB::table('restaurants')->where('id', $restaurant_id)->get();
return view ('restaurant.detail')->with('details', $details);
My question is: is there a better way to do this? I tried using the blade syntax without the #foreach and didn't have any luck.
I don't want to output this multiple times, the only reason I have the foreach there is because it is the only way I could get it to output.
If this is how it is supposed to work, no worries, I am just not familiar enough yet with blade to know if there is a better way to output this.
Thank you!
You seem to be selecting something by id, so that's probably unique. When you do ->get() by default it will return a collection of results because it assumes there's always a chance that there's more than 1. When selecting by ID however you know if something exists by that id, there's only going to be 1 of it. You can change the code to:
$detail = DB::table('restaurants')->where('id', $restaurant_id)->first();
return view ('restaurant.detail')->with('detail', $detail);
<html>
<head>
<meta charset="utf-8">
<title>{{ $detail->name }}</title>
</head>
<body>
<h1>in view</h1>
{{ $detail->name }}
<br>
{{ $detail->street }}
<br>
{{ $detail->city }}, {{ $detail->state }}. {{ $detail->zip }}
</body>
</html>
I highly recommend you look into Eloquent
Example of an eloquent model:
class Restaurant extends Model {} //The table name for model "Restaurant" is assumed to be restaurants
Then you can find a single restaurant:
$detail = Restaurant::find($restaurant_id);
Why do you want multiple title tags in your page ? It doesn't make any sense.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title><!-- What do you want to show here ? --></title>
</head>
<body>
<h1>in view</h1>
#foreach ($details as $detail)
{{ $detail->name }}
<br>
{{ $detail->street }}
<br>
{{ $detail->city }}, {{ $detail->state }}. {{ $detail->zip}}
#endforeach
</body>
</html>
You can achieve this by below methods,
1. Use first() to retrieve single record like below,
// Controller Code
$detail = DB::table('restaurants')->where('id', $restaurant_id)->first();
<!-- View Code -->
<title>{{ $detail->name }}</title>
2. Use find() to retrieve single record by its primary key like below. It works similar to first() but it retrieves data by its primary key.
// Controller Code
$detail = DB::table('restaurants')->find($restaurant_id);
<!-- View Code -->
<title>{{ $detail->name }}</title>
3. You can use get() as you did in your question but you need to access it as array with 0th index. I assumed that your query will retrieve only one record. It won't fail even if it retrieves multiple records but you will only first record in your view.
// Controller Code
$details = DB::table('restaurants')->where('id', $restaurant_id)->get();
<!-- View Code -->
<title>{{ $detail[0]->name }}</title>
There could be some more approaches but I used only these approaches so far.
I'm having problems with Laravel since i removed Entrust.
Everything was working fine until i tried installing Entrust. I removed it because it kept saying the following message the same as this question Laravel cache store does not support tagging
Laravel cache store does not support tagging
I removed everything to do with Entrust and removed any changes I had to make but since then whenever I try to go to another page after logging in, it redirects back to the login page. I log back in and it redirects to the dashboard as it should but as soon as i go to another page, it redirects me back to login again.
Any ideas how to fix this?
UPDATE
I think i've narrowed down to the problem.
I created a new project and started again. I created the Auth, created a couple test controllers and views.
I logged in as normal and I got redirected to the Home contoller. I then went to another page and it loaded fine as it should. The views are using the default layout in views/layouts/app.blade.php. As soon as I change it to a custom layout the problem occurs again but is okay if i change it back to app.blade.php
Here is my new default.blade.php but i don't see why this doesn't work anymore
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="csrf-token" content="{{ csrf_token() }}">
<title>#yield('title')</title>
<link rel="icon" type="image/png" href="public/css/favicon-32x32.png" sizes="32x32">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/themes/smoothness/jquery-ui.css">
{{ HTML::style('css/bootstrap.css') }}
#if (!empty($styles))
#foreach($styles as $style)
{{ HTML::style($style . '.css') }}
#endforeach
#endif
{{ HTML::style('css/app.css') }}
{{ HTML::style('css/media.css') }}
</head>
<body class="{{ $body_class or '' }}">
<div class="wrapper">
#include('layouts.header')
#yield('content')
<div class="push"></div>
</div>
<div class="footer navbar-primary">
<div class="row">
<div class="col-xs-12">
<footer>
<p>© Copyright #php echo date('Y'); #endphp Axminster Tools & Machinery</p>
</footer>
</div>
</div>
</div>
<script>
window.website_url = '{{ URL::to('/') }}';
</script>
{{ HTML::script('assets/src/jquery/dist/jquery.min.js') }}
{{ HTML::script('assets/src/jquery-ui/jquery-ui.min.js') }}
{{ HTML::script('js/bootstrap.min.js') }}
{{ HTML::script('js/validate.js') }}
#if(isset($jsVars))
<script>
#foreach($jsVars as $key => $val)
var {{ $key }} = {{ $val }};
#endforeach
</script>
#endif
<script>
$(function() {
$("#searchform").submit(function(e) {
$("#searchform").attr("action", "/search/term/" + encodeURI($("#keywords").val()));
});
});
</script>
#if (!empty($scripts))
#foreach($scripts as $script)
{{ HTML::script($script . '.js') }}
#endforeach
#endif
</body>
</html>
I eventually found the problem.
After doing a new installation i tested the Auth and it worked. As soon as I copied across some of my template files I found the problem occurred again. After looking at the template layout files, I found I was using this for the logout link
Change password
I did this within the first few days of learning Laravel and it was never a problem before...don't know why.
I changed it to this and it all works as expected
Logout
<form id="logout-form" action="{{ url('/logout') }}" method="POST" style="display: none;">
{{ csrf_field() }}
</form>
Im a beginner with laravel. Im following the laracasts tutorial and Im stuck at the part where you access another page with a form by:
{{ Form::open(['url' => 'created']) }}
for example.
Now that leads me to the right url but it gives me
Whoops, looks like something went wrong.
As soon as I type the link manually it works normally.
This is the code of the page where it directs to:
controller:
public function created()
{
return 'hello';
}
Routes:
Route::get('created', 'TestController#created');
View:
#extends('layout')
#section('content')
<h1> Test </h1>
#stop
This is the form of the 1st page:
#extends('layout')
#section('content')
<h1>Create New User</h1>
{{ Form::open(['url' => 'created']) }}
<div>
{{ Form::label('email', 'E-mail:')}}
{{ Form::text('email')}}
</div>
<div>
{{ Form::label('password', 'Password:')}}
{{ Form::password('password')}}
</div>
<div>
{{ Form::submit('Create')}}
</div>
{{ Form::close()}}
#stop
What is going wrong here?
Form open by default links to a post method so what you need is either a post route or a get method. Following should work:
{{ Form::open(['url' => 'created']) }}
// Insert your fields/codes here
{{ Form::close() }}
//Change route method to post
Route::post('created', 'TestController#created');
Please read the documentation here for more details.
I am trying to wrap my head around Laravel.
I am commenting code out for later use.
My problem occurs, when I have a snippet of code commented out, but the code is not "true".
Example for what works for me:
<head>
<!-- {{ HTML::script('js/scrollTo.js'); }} -->
{{ HTML::style('css/style.css'); }}
</head>
<head>
<!-- Foo {{ HTML::script('js/scrollTo.js'); }} Bar -->
{{ HTML::style('css/style.css'); }}
</head>
What doesn't work for me:
<head>
<!-- {{ } HTML::script('js/scrollTo.js'); }} -->
{{ HTML::style('css/style.css'); }}
</head>
Whenever I put something within the blade tags, the system makes an error.
Why I would even do this, i am not sure. But what I don't understand is why the system makes an error when the tag is commentet out.
Use Blade Comments:
<head>
{{-- {{ } HTML::script('js/scrollTo.js'); }} --}}
{{ HTML::style('css/style.css'); }}
</head>
The Blade tags still get rendered, even within an HTML comment (Blade is not a HTML parser, so it has no clue what <!-- means). Your resulting view code is going to be:
<!-- <?php } HTML::script('js/scrollTo.js'); ?> -->
which is a parse error. You can use Blade comments:
{{-- {{ } HTML::script('js/scrollTo.js'); }} --}}
but the real question is why not fix the slightly invalid code in the first place?
I don't know why, but it doesn't work because it is php. So whenever you "comment it out" it is still ran and will still return an error when the php is 'bad'. If you do want to comment them out then you can do it this way:
{{ dd($non_existent_value) }} <- causes error
<!-- {{ dd($non_existent_value) }} --> <- also causes error
{{ ''# dd($non_existent_value) }} <- echo's '', aka nothing
<!-- {{ ''# dd($non_existent_value) }} --> <- also echo's '', aka nothing
If you take a look at the code after you get errors on this you will see that with the first examples it tries to do <?php echo $non_existent_value ?>, with the latter it will do <?php echo '' # $non_existent_value ?> and thus echo '', or nothing.
Blade also provides a way to comment out the entire thing, just append '--' after the opening '{{' and before the closing '}}'. Like so:
{{-- dd($someVar) --}}
Both methods above are correct, although the first does not comment out the <?php echo ?> part but only what comes after 'echo'.
Ok, so a long title, but apologies, he only way to describe it without getting the wrong kind of answer.
So, the scenario....
I am creating a site which has a search form of sorts in the header, and therefore on every page. I would like it to retain its previous variables when being used for user convenience, for my convenience I have built the form into the default layout, to save recreating many instances of it.
default.blade.php (Heres the form, with unnecessary markup removed)
{{ Form::open(array('url' => '/search')) }}
{{ Form::select('model', Photo::getAvailableModels(true), $model) }}
{{ Form::select('colour', Photo::getAvailableColours(true), $colour) }}
{{ Form::submit('Go') }}
{{ Form::close() }}
The $model & $colour are variables I am capturing during the post. The problem is, I am getting unset variable errors from Blade on any pages where the user hasn't posted to, so I am literally having to preset them in almost every route or controller across my entire site, just to prevent the errors.
In essence, the system works fine as long as the user is posting, if someone visits the site using a direct link its basically useless.
Obviously I can not have the search form be set to the previously searched results, but that would be bad practice from a usability point of view.
Am I missing something here, surely there has to be a simple solution to this. Thanks for any help.
Create a view composer for your highly used variables:
View::composer(['store.index', 'products.*'], function($view)
{
$model = Input::get('model') ?: 'modelX';
$colour = Input::get('colour') ?: 'colourY';
$view->with('model', $model);
$view->with('colour', $colour);
});
Laravel will send those variables to your views automatically, every time someone hit one of them.
You can put that in your routes file, filters file or, like, me, create a app/composers.php and load by adding
require app_path().'/composers.php';
To your app/start/global.php.
You can check whether variables are set or not with PHP isset():
{{ Form::open(array('url' => '/search')) }}
#if (isset($model) && isset($colour))
{{ Form::select('model', Photo::getAvailableModels(true), $model) }}
{{ Form::select('colour', Photo::getAvailableColours(true), $colour) }}
#else
{{ Form::select('model', Photo::getAvailableModels(true)) }}
{{ Form::select('colour', Photo::getAvailableColours(true)) }}
#endif
{{ Form::submit('Go') }}
{{ Form::close() }}
Or, if your form fields are optional and you need to check separately:
{{ Form::open(array('url' => '/search')) }}
#if (isset($model))
{{ Form::select('model', Photo::getAvailableModels(true), $model) }}
#else
{{ Form::select('model', Photo::getAvailableModels(true)) }}
#endif
#if (isset($colour))
{{ Form::select('colour', Photo::getAvailableColours(true), $colour) }}
#else
{{ Form::select('colour', Photo::getAvailableColours(true)) }}
#endif
{{ Form::submit('Go') }}
{{ Form::close() }}