$\displaystyle
(y^2-xe^{x^2}) dx + (2xy+cos(y)) dy = 0
$
First you need to check if $\displaystyle \frac{\partial}{\partial y}(y^2 - x\,e^{x^2}) = \frac{\partial}{\partial x}(2xy + \cos{y})$.
$\displaystyle \frac{\partial}{\partial y}(y^2 - x\,e^{x^2}) = 2y$.
$\displaystyle \frac{\partial}{\partial x}(2xy + \cos{y}) = 2y$.
So the condition is satisfied. Since this is true, that means $\displaystyle \frac{\partial f}{\partial x} = y^2 - x\,e^{x^2}$ and $\displaystyle \frac{\partial f}{\partial y} = 2xy + \cos{y}$.
Since $\displaystyle \frac{\partial f}{\partial x} = y^2 - x\,e^{x^2}$
$\displaystyle f(x, y) = \int{y^2 - x\,e^{x^2}\,dx}$
$\displaystyle = xy^2 - \frac{1}{2}e^{x^2} + g(y)$.
Since $\displaystyle \frac{\partial f}{\partial y} = 2xy + \cos{y}$
$\displaystyle f(x, y) = \int{2xy + \cos{y}\,dy}$
$\displaystyle = xy^2 + \sin{y} + h(x)$.
Putting these together, we can see that
$\displaystyle f(x, y) = xy^2 - \frac{1}{2}e^{x^2} + \sin{y} + C$ where $\displaystyle C$ is a constant.